Cookies

Sessions (session_start();) store information about the user as long as the browser is not closed, but that information is lost once the user closes the browser.

Cookies on the other hand can be used to keep users logged into our website after they close their browser.

Setting cookies

Cookies are set by using function setcookie() and you pass to it the variable name, variable value, and when you want the cookie to expire. For e.g. if you want the cookie to expire in 24 hours you would do something like:

<?php

setcookie("$uname", $_POST["username"], ( time() + ( 60 * 60 * 24 ) ) );

?>

Accessing cookies

You would access a cookie variable as you do the $_GET, $_POST, $_SESSION variable. For example

$_COOKIE["$uname"]

Deleting cookies

You would typically delete cookies once the user logs out. Cookies are deleted by setting the expration time of the cookie to a time in the past. This change will not be effective until the next load of the page. For example:

setcookie("$uname", "", (time() - (60 * 60) ) );

Updating cookie value

Cookie values are updated like any other PHP variable. For e.g.

$_COOKIE["$uname"] = "test";