PHP setcookie() Fungsi
Contoh
Contoh berikut membuat cookie bernama "pengguna" dengan nilai "John Doe". Cookie akan kedaluwarsa setelah 30 hari (86400 * 30). Tanda "/" berarti bahwa cookie tersedia di seluruh situs web (jika tidak, pilih direktori yang Anda inginkan).
Kami kemudian mengambil nilai cookie "pengguna" (menggunakan variabel global $_COOKIE). Kami juga menggunakan fungsi isset() untuk mengetahui apakah cookie disetel:
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Definisi dan Penggunaan
Fungsi setcookie() mendefinisikan cookie yang akan dikirim bersama dengan header HTTP lainnya.
Cookie sering digunakan untuk mengidentifikasi pengguna. Cookie adalah file kecil yang disematkan server di komputer pengguna. Setiap kali komputer yang sama meminta halaman dengan browser, cookie juga akan dikirim. Dengan PHP, Anda dapat membuat dan mengambil nilai cookie.
Nama cookie secara otomatis ditetapkan ke variabel dengan nama yang sama. Misalnya, jika cookie dikirim dengan nama "pengguna", sebuah variabel secara otomatis dibuat bernama $user, yang berisi nilai cookie.
Catatan: Fungsi setcookie() harus muncul SEBELUM tag <html>.
Catatan: Nilai cookie secara otomatis dikodekan URL saat mengirim cookie, dan secara otomatis didekode saat diterima (untuk mencegah pengkodean URL, gunakan setrawcookie() sebagai gantinya).
Sintaksis
setcookie(name, value, expire, path, domain, secure, httponly);
Nilai Parameter
Parameter | Description |
---|---|
name | Required. Specifies the name of the cookie |
value | Optional. Specifies the value of the cookie |
expire | Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0 |
path | Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in |
domain | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain |
secure | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE |
httponly | Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE |
Detail Teknis
Nilai Kembali: | BENAR pada kesuksesan. SALAH pada kegagalan |
---|---|
Versi PHP: | 4+ |
Log Perubahan PHP: | PHP 5.5 - Atribut Max-Age disertakan dalam header Set-Cookie yang dikirim ke klien PHP 5.2 - Parameter httponly telah ditambahkan |
Lebih Banyak Contoh
Contoh
Beberapa tanggal kedaluwarsa untuk cookie:
<?php
$value = "Hello world!";
// cookie will expire when the browser close
setcookie("myCookie", $value);
// cookie will expire in 1 hour
setcookie("myCookie", $value, time() + 3600);
// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie("myCookie", $value, time() + 3600, "/php/");
?>
<html>
<body>
...some code...
</body>
</html>
Contoh
Untuk memodifikasi cookie, cukup setel (lagi) cookie menggunakan fungsi setcookie() :
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Contoh
Untuk menghapus cookie, gunakan fungsi setcookie() dengan tanggal kedaluwarsa di masa lalu:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Contoh
Buat skrip kecil yang memeriksa apakah cookie diaktifkan. Pertama, coba buat cookie uji dengan fungsi setcookie(), lalu hitung variabel array $_COOKIE:
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
Referensi Jaringan PHP