Perintah Kawalan Pergerakan (PKP) hari ke-115

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

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

Delete Cookies

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

Set Sessions

<?php
// Start the session
session_start();
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
?>

Destroy Sessions

<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>

Penerangan lanjut di

  1. https://www.w3schools.com/php/php_cookies.asp
  2. https://www.w3schools.com/php/php_sessions.asp

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.