PHP setrawcookie() Fungsi
Contoh
Contoh berikut membuat cookie dengan PHP. Cookie diberi nama "pengguna" dan nilainya akan menjadi "John Doe". Nilai cookie tidak akan dikodekan URL. Cookie akan kedaluwarsa setelah 30 hari (86400 * 30). Menggunakan "/", berarti cookie tersedia di seluruh situs web (jika tidak, pilih direktori yang Anda inginkan):
<?php
$cookie_name = "user";
$cookie_value = "John";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>
<?php
echo "Cookie is set.";
?>
</body>
</html>
?>
Definisi dan Penggunaan
Fungsi setrawcookie() mendefinisikan cookie (tanpa pengkodean URL) untuk 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 setrawcookie() harus muncul SEBELUM tag <html>.
Catatan: Untuk secara otomatis mengkodekan URL nilai cookie saat mengirim, dan secara otomatis mendekode saat menerima, gunakan fungsi setcookie() sebagai gantinya.
Sintaksis
setrawcookie(name, value, expire, path, domain, secure);
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 not set, the cookie will expire at the end of the session (when the browser closes) |
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. |
Detail Teknis
Nilai Kembali: | BENAR pada kesuksesan. SALAH pada kegagalan |
---|---|
Versi PHP: | 5+ |
Lebih Banyak Contoh
Contoh
Ambil nilai cookie bernama "pengguna" (menggunakan variabel global $_COOKIE). Gunakan juga fungsi isset() untuk mengetahui apakah cookie itu ada:
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Contoh
Untuk memodifikasi cookie, cukup setel (lagi) cookie menggunakan fungsi setrawcookie() :
<?php
$cookie_name = "user";
$cookie_value = "Alex";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Contoh
Untuk menghapus cookie, gunakan fungsi setrawcookie() dengan tanggal kedaluwarsa di masa lalu:
<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', 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 setrawcookie(), lalu hitung variabel array $_COOKIE:
<?php
setrawcookie("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