You are on page 1of 3

/*PROGRAM FOR ILLUSTRATE THE CONCEPT OF COOKIES*/

cookies.php
<?php
$user=$_POST['user'];
$color=$_POST['color'];
$self=$_SERVER['PHP_SELF'];
if(($user!=null)and($color!=null))
{
setcookie("firstname",$user,time()+86400);
setcookie("fontcolor",$color,time()+86400);
header("Location:cookies2.php");
exit();
}
?>
<html>
<head>
<title>Creating cookies</title>
</head>
<body>
<h1>Creating cookies</h1>
<hr>
<form action="cookies.php" method="post">
Please Enter Your First Name:
<input type="text" name="user"><br><br><br>
Please Choose Your Favorite Font Color:<br>
<input type="radio" name="color" value="purple">purple<br>
<input type="radio" name="color" value="pink">pink<br>
<input type="radio" name="color" value="blue">blue<br><br><br>
<input type="submit" value="submit"></form></hr></body></html>
cookies2.php
<?php
if(isset($_COOKIE['firstname']))
{
$user=$_COOKIE['firstname'];
$color=$_COOKIE['fontcolor'];
}
else {
$user=$_POST['user'];
$color=$_POST['color'];
}
?>
<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<h1>Reading Cookies</h1>
<h2>HELLO:<?php echo($user);?></h2>
<h2>Your color:<?php echo($color);?></h2>
<hr>
<br>
<?php
echo("<br/>");
echo("<pre>");
echo("cookie info:\n");
print_r($_COOKIE);
echo("</pre>");
?>
<a><input type="button" value="back" onclick="history.go(-1)"></a>
</body></html>
OUTPUT: