Tutorial Number 10 Web Skills: Passing Information From One Page To Another Will Cookies and Ssessions. Cookies
Tutorial Number 10 Web Skills: Passing Information From One Page To Another Will Cookies and Ssessions. Cookies
Cookies:
Syntax:
setcookie(name, value, expire, path, domain);
Task No: 1
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<html>
<body>…
Task Number 2
Retrieving a cookie
Cookies Page 1
The PHP $_COOKIE variable is used to retrieve a cookie
value.
Example
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
Example 2
<html>
<body>
<?php
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
else
echo "Welcome guest!<br />";
?>
</body>
</html>
Task number 3
Deleting a Cookie
?>
Cookies Page 2
Saving cookies and checking if the user has revisited the
page or is a first timer.
<?php
if (!isset($_COOKIE['visited']))
{
// if a cookie does not exist
// set it
setcookie("visited", "1", mktime()+86400, "/") or
die("Could not set cookie");
echo "This is your first visit here today.";
}
else
{
// if a cookie already exists
echo "Nice to see you again, old friend!";
}
?>
Cookies Page 3