Tutorial PHP

RUMAH PHP Pengenalan PHP Instal PHP Sintaks PHP Komentar PHP Variabel PHP PHP Gema / Cetak Tipe Data PHP String PHP Nomor PHP PHP Matematika Konstanta PHP Operator PHP PHP Jika...Lain...Elseif Beralih PHP PHP Loop Fungsi PHP Array PHP PHP Superglobal PHP RegEx

Formulir PHP

Penanganan Formulir PHP Validasi Formulir PHP Diperlukan Formulir PHP URL/Email Formulir PHP Formulir PHP Lengkap

PHP Lanjutan

Tanggal dan Waktu PHP PHP Termasuk Penanganan File PHP File PHP Buka/Baca Membuat/Menulis File PHP Unggah File PHP Cookie PHP Sesi PHP Filter PHP Filter PHP Tingkat Lanjut Fungsi Panggilan Balik PHP PHP JSON Pengecualian PHP

PHP OOP

PHP Apa itu OOP Kelas/Objek PHP Konstruktor PHP Penghancur PHP Pengubah Akses PHP Warisan PHP Konstanta PHP Kelas Abstrak PHP Antarmuka PHP Ciri-ciri PHP Metode Statis PHP Properti Statis PHP Ruang Nama PHP PHP Iterable

Database MySQL

Database MySQL Koneksi MySQL MySQL Buat DB MySQL Buat Tabel MySQL Sisipkan Data MySQL Dapatkan ID Terakhir MySQL Sisipkan Beberapa MySQL Disiapkan MySQL Pilih Data MySQL Dimana MySQL Dipesan Oleh MySQL Hapus Data Data Pembaruan MySQL Data Batas MySQL

PHP XML

PHP XML Parser PHP SimpleXML Parser PHP SimpleXML - Dapatkan PHP XML Ekspat PHP XML DOM

PHP - AJAX

Pengenalan AJAX AJAX PHP Basis Data AJAX AJAX XML Pencarian Langsung AJAX Jajak Pendapat AJAX

Contoh PHP

Contoh PHP Kompilator PHP Kuis PHP Latihan PHP Sertifikat PHP

Referensi PHP

Ikhtisar PHP Array PHP Kalender PHP Tanggal PHP Direktori PHP Kesalahan PHP Pengecualian PHP Sistem File PHP Filter PHP PHP FTP PHP JSON Kata Kunci PHP PHP Libxml Surat PHP PHP Matematika PHP Lain-lain PHP MySQLi Jaringan PHP Kontrol Keluaran PHP PHP RegEx PHP SimpleXML Aliran PHP String PHP Penanganan Variabel PHP PHP XML Parser PHP Zip Zona Waktu PHP

PHP setrawcookie() Fungsi

Referensi Jaringan PHP

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