How to read from cookies in PHP?
You read cookies’ value the same as you have been reading POST store method. The following is the format:
$_COOKIE[‘name’]; Remember that the cookies’values are accessible to all scripts in your application except the one that sent it.
setcookie (‘name’, ‘Borna and Dana’);
print $_COOKIE[‘name’];
The value on print will be null.
Now, let us write a PHP to view all user’s information. Type the following code into notepad and save it as “view_cookies.php” then upload it to your web server.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitionl.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>View user information from cookies</title>
</head>
<body>
<?php
// always have your error handling turn on.
ini_set (‘display_errors’,1);
error_reporting (E_ALL & ~E_NOTICE);
// check for user information
if (isset ($_COOKIE[‘name’])) {
print “Name: {$_COOKIE[‘name’]};\n”
}
if (isset ($_COOKIE[‘ssn’])) {
print “Social Security Number: {$_COOKIE[‘ssn’]};\n”
}
if (isset ($_COOKIE[‘phone’])) {
print “Phone: {$_COOKIE[‘phone’]};\n”
}
if (isset ($_COOKIE[‘email’])) {
print “email address: {$_COOKIE[‘email’]};\n”
}
?>
</body>
</html>
|