Forum Moderators: coopster
Read the info in the Sessions and Cookies sections to get a feel for the differences.
Try some tests. Like this one:
<?php
# IF NO COOKIE, yet, AND FORM HASN'T BEEN SUBMITTED
if (!$_COOKIE['testcookie'] &&!$user ) {
#SHOW THE "USER" FORM
print("<form action=\"");
echo $_SERVER["PHP_SELF"];
#THIS WILL PLACE THE VARIABLE "USER" INTO THE URL
print("\">User: <input type=\"text\" name=\"user\">");
print("<input type=\"submit\" value=\"Set Cookie\">");
print("</form>");
}
else {
#STILL NO COOKIE...BUT THE FORM HAS BEEN SUBMITTED
if (!$_COOKIE['testcookie']) {
#MAKE A DISTANT EXPIRATION DATE (SETS THE COOKIE 'FOREVER')
$expdate=mktime(0,0,0,1,1,2038);
#SET THE COOKIE (name of cookie,cookie value,expire date)
setcookie("testcookie",$user,$expdate);
}
#SHOW WHAT THE VALUE OF THE NEW COOKIE IS
print_r($_COOKIE['testcookie']);
}
?>
Save that to a file, upload it to your PHP-enabled web server, and open it in your browser.
At first, you should see the "User" form, where you will enter your user name. When you submit the form, you will come back to the same page which will now display the "value" of the cookie "testcookie" which is set to "expire" Jan. 1, 2038 (the Windows max date).
Have fun!