0% found this document useful (0 votes)
108 views5 pages

Cookies PDF

The document discusses techniques for maintaining state in web applications. It describes how HTTP is stateless and various methods like hidden form fields, cookies, sessions that are used to track user sessions and state across multiple requests. It also provides details on how cookies work, how to set cookies using PHP and access cookie values. Finally, it presents an example problem on creating a student marksheet application using cookies to pass data across forms.

Uploaded by

vsh36368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views5 pages

Cookies PDF

The document discusses techniques for maintaining state in web applications. It describes how HTTP is stateless and various methods like hidden form fields, cookies, sessions that are used to track user sessions and state across multiple requests. It also provides details on how cookies work, how to set cookies using PHP and access cookie values. Finally, it presents an example problem on creating a student marksheet application using cookies to pass data across forms.

Uploaded by

vsh36368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 1.

Web Techniques
Maintaining State

 HTTP is a stateless protocol, which means that once a web server completes a client's
request for a web page, the connection between the two goes away.
 In other words, there is no way for a server to recognize that a sequence of requests all
originate from the same client. State is useful, though.
 You can't build a shopping-cart application, for example, if you can't keep track of a
sequence of requests from a single user. You need to know when a user puts a item in
his cart, when he adds items, when he removes them, and what's in the cart when he
decides to check out.
 To get around the Web's lack of state, programmers have come up with many tricks to
keep track of state information between requests (also known as session tracking ).
 Such technique is
o To use hidden form fields to pass around information. PHP treats hidden form
fields just like normal form fields, so the values are available in the $_GET and
$_POST arrays. Using hidden form fields, you can pass around the entire
contents of a shopping cart.
o To assign each user a unique identifier and pass the ID around using a single
hidden form field. While hidden form fields work in all browsers, they work only
for a sequence of dynamically generated forms, so they aren't as generally useful
as some other techniques.
o Use URL rewriting, where every local URL on which the user might click is
dynamically modified to include extra information. This extra information is
often specified as a parameter in the URL. For example, if you assign every user a
unique ID, you might include that ID in all URLs. URL rewriting works for all
dynamically generated documents, not just forms, but actually performing the
rewriting can be tedious.
o A third technique for maintaining state is to use cookies. Cookies: A cookie is a
bit of information that the server can give to a client. On every subsequent
request the client will give that information back to the server, thus identifying
it. Cookies are useful for retaining information through repeated visits by a
browser, but they're not without their own problems. The main problem is that
some browsers don't support cookies, and even with browsers that do, the user
can disable cookies.
o The best way to maintain state with PHP is to use the built-in session-tracking
system. This system lets you create persistent variables that are accessible from
different pages of your application, as well as in different visits to the site by the
same user. PHP's session-tracking mechanism uses cookies (or URLs) to
elegantly solve most problems that require state, taking care of all the details for
you.

Cookies:

Definition: Cookie is a small amount of data stored by the user’s browser in compliance with a
request from a server or script.
A cookie is basically a string that contains several fields. A server can send one or more cookies
to a browser in the headers of a response. Some of the cookie's fields indicate the pages for
which the browser should send the cookie as part of the request. The value field of the cookie is
the payload—servers can store any data they like there (within limits), such as a unique code
identifying the user, preferences, etc.

Use the setcookie( ) function to send a cookie to the browser:

setcookie(name [, value [, expire [, path [, domain [, secure ]]]]]);

 This function creates the cookie string from the given arguments and creates a Cookie
header with that string as its value.
 Because cookies are sent as headers in the response, setcookie( ) must be called before
any of the body of the document is sent.
 The parameters of setcookie( ) are:
o name: A unique name for a particular cookie. You can have multiple cookies
with different names and attributes. The name must not contain whitespace or
semicolons value.
o value: The arbitrary string value attached to this cookie.
o expire: The expiration date for this cookie. If no expiration date is specified, the
browser saves the cookie in memory and not on disk. When the browser exits,
the cookie disappears.
o path: The browser will return the cookie only for URLs below this path. The
default is the directory in which the current page resides. For example, if
/store/front/cart.php sets a cookie and doesn't specify a path, the cookie will be
sent back to the server for all pages whose URL path starts with /store/front/.
o domain: The browser will return the cookie only for URLs within this domain. The
default is the server hostname. The browser will transmit the cookie only over
https connections.
o secure: The default is false, meaning that it's okay to send the cookie over
insecure connections.
When a browser sends a cookie back to the server, you can access that cookie through the
$_COOKIE array. The key is the cookie name, and the value is the cookie's value field.

For instance, the following code at the top of a page keeps track of the number of times the
page has been accessed by this client:

<?php

print_r($_COOKIE);

if(!isset($_COOKIE['accesses']))

setcookie('accesses',0);

else

$page_accesses=$_COOKIE['accesses'];

setcookie('accesses',++$page_accesses);

echo "This page is accesses".$page_accesses;

?>

When decoding cookies, any periods (.) in a cookie's name are turned into underscores.

For instance, a cookie named tip.top is accessible as $_COOKIE['tip_top'].

Programs on Cookies
Problem Statement: Create a form to accept student information (name, class, address).Once
the student information is accepted, accept marks in next form
(Phy, Bio , Chem, Maths, Marathi, English) .Display the mark sheet
for the student in the next form containing name, class, marks of the
subject, total and percentage.

Solution:
Page 1: Student_example_cookies.html

<html>
<head>
<title>
Student Personal Information
</title>
</head>
<form action="student_example_cookies1.php" method="GET">
Enter the Name <input type=text name=sname><br>
Enter the Roll_NO <input type=text name=rno><br>
Enter the Class <input type=text name=class><br>
Enter the Semester <input type=text name=sem><br>
<input type="submit" Value="Submit">
</form>
</html>

Page 2: Student_example_cookies1.php

<html>
<head>
<title>
Student Marks Information
</title>
</head>
<form action="student_example_cookies2.php" method="GET">
Enter the SYSPRO Marks <input type=text name=syspro><br>
Enter the TCS Marks<input type=text name=tcs><br>
Enter the CN Marks<input type=text name=cn><br>
Enter the JAVA Marks <input type=text name=java><br>
Enter the IP Marks <input type=text name=ip><br>
Enter the CG Marks <input type=text name=cg><br>
<input type="SUBMIT" value="SUBMIT">
</form>
<?php
setcookie("sname",$_GET['sname']);
setcookie("rno",$_GET['rno']);
setcookie("class",$_GET['class']);
setcookie("sem",$_GET['sem']);
?>
</html>
Page 3: Student_example_cookies2.php

Note: - Third Page task is to be completed by students.


Hint:- Use $_COOKIE[‘keyname’] & $_GET[‘key’] to display marksheet.
Problem Statement2: Count Number of time the page is access using cookies.
Solution:
<?php
print_r($_COOKIE);
if(!isset($_COOKIE['accesses']))
{
setcookie('accesses',0);
}
else
{
$page_accesses=$_COOKIE['accesses'];
setcookie('accesses',++$page_accesses);
echo "This page is accesses".$page_accesses;
}
?>

References:

https://docstore.mik.ua/orelly/weblinux2/php/ch07_06.htm

You might also like