Professional Documents
Culture Documents
Chap4 Cookies
Chap4 Cookies
Chap4 Cookies
Cookies:
Cookie is a small piece of data that a server sends to the user's web browser. The
browser may store it and send it back with the next request to the same
server. Typically, it's used to tell if two requests came from the same browser —
keeping a user logged-in, for example. It remembers stateful information for
the stateless HTTP protocol.
Cookie is created at server side and saved to client browser. Each time when client
sends request to the server, cookie is embedded with request. Such way, cookie can
be received at the server side.
In short, cookie can be created, sent and received at server end. Each time the
browser requests a page to the server, all the data in the cookie is automatically
sent to the server within the request
Http is a stateless protocol; cookies allow us to track the state of the application
using small files stored on the user’s computer.
The path where the cookies are stored depends on the browser.
Example-
1. Cookies are simple to use and implement, they do not need any server
resource.
2. Occupies less memory, do not require any server resources and are stored
on the user's computer so no extra burden on server.
3. We can configure cookies to expire when the browser session ends
(session cookies) or they can exist for a specified length of time on the
client’s computer (persistent cookies). Cookies persist a much longer
period of time than Session state.
4. The cookies are stored on the client’s hard disk , so , if server crashes
the cookies are still available .
1. Cookies are not secure as they are stored in clear text, they may pose a possible
security risk as anyone can open and tamper with cookies. You can manually
encrypt and decrypt cookies, but it requires extra coding and can affect
application performance because of the time that is required for encryption and
decryption
2. Several limitations exist on the size of the cookie text (4kb in general), number
of cookies (20 per site in general), etc.
3. User has the option of disabling cookies on his computer from browser’s
setting .
· Cookies will not work if the security level is set to high in the browser.
· Users can delete a cookies.
· Users browser can refuse to use cookies
· Complex type of data not allowed (e.g. dataset etc). It allows only plain
text (i.e. cookie allows only string content)
If you wish to retreive all the cookies, you may use the following command
<?php
print_r($_COOKIE);
?>
Programs using Cookies
1. Write a PHP script to create and view the content of cookie [Hint:
setcookie() and $_COOKIE]
Solution:
<?php
setcookie("sybca","PHP Demo of Cookie", time()+60);
echo $_COOKIE["sybca"];
?>
<html>
<head><title>Cookie Demo</title></head>
<body>
<h1> Hello Students</h1>
</body>
</html>
2. Write a script to keep track of number of times the web page has been
accessed [Hint: Use $_COOKIE].
Solution:
<?php
if(isset($_COOKIE['bca']))
{
$x=$_COOKIE['bca'];
$x=$x+1;
setcookie('bca',$x);
echo "you accessed this page $x times";
}
else
{
$x=1;
setcookie('mca',$x);
echo "you accessed this page for $x time";
}
?>
3. Create a form to accept employee details like name, address and mobile
number. Once the employee information is accepted, then accept LIC
information like policy_no, name, premium. Display employee details and
LIC details on next form.(use COOKIE)
Solution
Emp.html
<html>
<body>
<form action="/php programs/lic.php" method="post">
<center>
<h2>Enter Employee Details :</h2>
Name :<input type="text" name="nm"><br><br>
Address :<input type="text" name="addr"><br><br>
Mobile No : </td><td><input type="text" name="mno"><br><br>
<br><input type="submit" value="Next">
</center>
</form>
</body>
</html>
Lic.php
<html>
<body>
<form action="/php programs/emp-policy.php" method="post">
<center>
<h2>Enter LIC Information :</h2>
<br>Policy no. : </td><td><input type="text" name="pno">
<br>Policy Name : </td><td><input type="text" name="pnm">
<br>Premium : </td><td><input type="text" name="premium">
<br><input type="submit" value="Next">
</center>
</form>
</body>
</html>
<?php
setcookie("emp1",$_POST['nm'],time()+3600);
setcookie("emp2",$_POST['addr'],time()+3600);
setcookie("emp3",$_POST['mno'],time()+3600);
?>
Emp-policy.php
<?php
echo"<center>";
echo "<h3>Employee Details</h3> ";
echo "<b>Name : </b>".$_COOKIE['emp1']."<br>";
echo "<b>Address : </b>".$_COOKIE['emp2']."<br>";
echo "<b>Mobile No. : </b>".$_COOKIE['emp3']."<br>";
echo "<b>Policy no. : </b>".$_POST['pno']."<br>";
echo "<b>Policy Name : </b>".$_POST['pnm']."<br>";
echo "<b>Premium : </b>".$_POST['premium']."<br>";
echo"</center>";
?>
4. Change the preferences of your web page like font style, alignment, font size,
font color, background color using cookie. Display selected settings on next web
page and actual implementation (with new settings) on third web page.
Solution:
Settings.html
<html>
<body>
<form action="/php programs/format.php" method="get">
<center>
<b>Select font Alignment :</b><input type="text" name="t1"><br><br>
<b>Enter font size : </b><input type="text" name="t2"><br><br>
<b>Enter Font Color :</b><input type="text" name="t3"><br><br>
<b>Enter background color :</b> <input type="text" name="t4"><br><br>
<input type="submit" value="Next">
</center>
</form>
</body>
</html>
Format.php
<?php
echo "<br>Alignment is ".$_GET['t1'];
echo "<br>Background color is ".$_GET['t4'];
echo "<br>Font Color is",$_GET['t3'];
echo" <br>size is ".$_GET['t2'];
setcookie("set1",$_GET['t1'],time()+3600);
setcookie("set2",$_GET['t2'],time()+3600);
setcookie("set3",$_GET['t3'],time()+3600);
setcookie("set4",$_GET['t4'],time()+3600);
?>
<html>
<body>
<form action="/php programs/implementation.php">
<input type=submit value="OK">
</form>
</body>
</html>
Implementation.php
<?php
$style=$_COOKIE['set1'];
$size=$_COOKIE['set2'];
$color=$_COOKIE['set3'];
$bcolor=$_COOKIE['set4'];
$msg="Hello Students";
echo "<body style='text-align:$style;color:$color;background-color:$bcolor;size:
$size'>$msg</body>";
?>
5. Create a form to accept student information (name, class, address).
Once the student information is accepted, accept marks in next form
(Java, PHP, ST, IT, pract1, and project). Display the mark sheet for
the student in the next form containing name, class, marks of the
subject, total and percentage(Use $_COOKIE).
Solution:
Stud.html
<html>
<body>
<form action="/php programs/marks.php" method="post">
<center>
<h2>Enter Students information :</h2>
<br>Name : </td><td><input type="text" name="name"></br></br>
<br>Address : </td><td><input type="text" name="addr"></br></br>
<br>Class :<input type="text" name="class"></br></br>
<input type="submit" value=Next></br></br>
</center>
</form>
</body>
</html>
Marks.php
<html>
<body>
<form action="/php programs/marksheet.php" method="post">
<center>
<h2>Enter Marks for Student:</h2>
<br>Java : <input type="text" name="m1"><br>
<br>PHP : <input type="text" name="m2"></br>
<br>ST : <input type="text" name="m3"><br>
<br>IT : <input type="text" name="m4"></br>
<br>Practical : <input type="text" name="m5"></br>
<br>Project : <input type="text" name="m6"></br></br>
<br><input type="submit" value=Next></br></br>
</center>
</form>
</body>
</html>
<?php
setcookie("stud1",$_POST['name'],time()+3600);
setcookie("stud2",$_POST['addr'],time()+3600);
setcookie("stud3",$_POST['class'],time()+3600);
?>
Marksheet.php
<?php
echo "<h3>Marksheet</h3> ";
echo "<br>Name : ".$_COOKIE['stud1'];
echo "<br>Address : ".$_COOKIE['stud2'];
echo "<br>Class : ".$_COOKIE['stud3'];
echo "<br>Java : ".$_POST['m1'];
echo "<br>PHP : ".$_POST['m2'];
echo "<br>ST : ".$_POST['m3'];
echo "<br>IT : ".$_POST['m4'];
echo "<br>Practical : ".$_POST['m5'];
echo "<br>Project : ".$_POST['m6'];
?>
4. Create student registration form and display details in the next page. [use
cookies]
Registration .php
<html>
<body>
<form action="/php programs/show.php" method=post>
<br>Enter Name<input type="text" name="nm" >
<br>Enter Roll No<input type="text" name="rno" >
<brEnter State<input type="text" name="st" >
<br>Enter City<input type="text" name="ct">
<br>Enter Percentage<input type="text" name="per" >
<br><input type="submit" value="Submit" name="submit"></form>
</html>
<?php
if(isset($_POST['submit']))
{
$nm=$_POST['nm'];
$rno=$_POST['rno'];
$st=$_POST['st'];
$ct=$_POST['ct'];
$perc=$_POST['per'];
}
if((!empty($nm)) && (!empty($rno)) && (!empty($st)) && (!empty($ct)) && (!
empty($perc)))
{
setcookie('nm',$nm);
setcookie('rno',$rno);
setcookie('st',$st);
setcookie('ct',$ct);
setcookie('perc',$perc);
}
?>
</body>
</html>
show.php
<?php
echo "<br>Your name is $_COOKIE[nm] ";
echo "<br>Your Roll No Is : $_COOKIE[rno]";
echo "<br>Your State Is : $_COOKIE[st]";
echo "<br>Your City Is : $_COOKIE[ct]";
echo "<br>Your Percentage Is : $_COOKIE[perc]";
?>