PHP Lesson: Cookies & Sessions

PHP Cookies dan PHP Sessions digunakan bagi menyimpan data pengguna. Bagaimana pun terdapat perbezaan. Cookies menyimpan data pada web browser atau pada computer pengguna. Sessions menyimpan data pada web server. Sessions akan hilang apabila browser ditutup manakala cookies tidak hilang apabila browser ditutup dan boleh digunakan semula apabila browser dibuka kembali.

Syntax Cookies

setcookie(name, value, expire, path, domain, secure, httponly);

 Create Cookies

// set the expiration date for 30 days
setcookie("user", "Shahrul", time() + (86400 * 30), "/"); // 86400 = 1 day

Delete Cookies

// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);

Set Sessions

// Start the session
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";

Destroy Sessions

// remove all session variables
// destroy the session

Penerangan lanjut di


Leave a Reply

Your email address will not be published. Required fields are marked *