Cookie
header
Set-Cookie
header in the
HTTP response
setcookie(cookieName, cookieValue, [expirationDate])
// Create "session cookie" which expires when browser is closed setcookie("myName", "Bobby Bison"); // Create "persistent cookie" which expires in 15 seconds setcookie("myAge", 21, time() + 15);
Set-Cookie: myName=Bobby+Bison Set-Cookie: myAge=21; expires=Mon, 29-Oct-2018 22:32:06 GMT
$_COOKIES
// Create session cookie which expires when browser is closed if (isset($_COOKIE["myName"])) echo "myName is $_COOKIE[myName]<br>"; if (isset($_COOKIE["myAge"])) echo "myAge is $_COOKIE[myAge]";
Cookie: myName=Bobby+Bison; myAge=21
$_COOKIES
cannot be used for setting cookies!
// WRONG! Does not actually set any cookies on the browser $_COOKIE["myName"] = "Wild Bill";
setcookie()
does not immediately
put anything in $_COOKIES
setcookie("myName") = "Susan"; // ERROR! Not set until cookies are sent back from the browser! echo $_COOKIE["myName"];
// Displays user's session ID (Example: 556mkqe25ja90lnfsskij325t0) echo session_id();
session_start()
at top of script
// Creates a session ID if one doesn't already exist session_start(); $_SESSION["myName"] = "Bobby Bison"; $_SESSION["myAge"] = 21;
session_start()
at top of script
// Creates a session ID if one doesn't already exist session_start(); if (isset($_SESSION["myName"])) echo "myName is $_SESSION[myName]<br>"; if (isset($_SESSION["myAge"])) echo "myAge is $_SESSION[myAge]";
session_start()
at top of script
session_destory()
to remove all variables on the server
// Creates a session ID if one doesn't already exist session_start(); // Clear all session variables session_destroy();