PHP/Cookie Session/Cookie
Creating a Test Cookie
<?php
$string_name = " testcookie";
$string_value = "This is a test cookie";
$expiry_info = time()+259200;
$string_domain = "localhost.localdomain";
setcookie($string_name, $string_value, $expiry_info, $string_domain);
?>
Use cookie to create page counter
<?php
if (!isset($_COOKIE["visits"])) $_COOKIE["visits"] = 0;
$visits = $_COOKIE["visits"] + 1;
setcookie("visits",$visits,time()+3600*24*365);
?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?php
if ($visits > 1) {
echo("This is visit number $visits.");
} else { // First visit
echo("Welcome to my Website! Click here for a tour!");
}
?>
</body>
</html>
Using Multiple Values for a Cookie
<?php
if (!isset ($userdetails [0] ) ) {
setcookie ("userdetails[0]", $username);
}
$userdetails[1]++;
setcookie ("userdetails[1]", $userdetails[1]);
echo ("Hello $userdetails[0], you"ve seen this page".$userdetails[1].($userdetails[1] == l?" time!": "times!"));
?>