Cookies and Sessions
Cookies and Sessions
• We will also discuss the ways in which our PHP scripts can
deal with incoming information from HTML pages and URL
queries.
2
Statelessness - 1
• The problem with HTTP as a delivery platform is that it is
stateless.
– The only data you have in the form is the data you take with you.
3
Statelessness - 2
• HTTP permits the sending of data to web pages.
- This data is however not sent onto other pages due to the
statelessness of the protocol.
• Two methods for this are provided:
- GET
- POST
• When it is time to send information (for example, from form
elements), it is encoded by the client and then sent in one
of these two ways.
4
GET
• Using the GET method, the information that is encoded
gets sent as an extension to the URL.
- It will appear as something like:
http://<url>/dice_roll_get.php?num=6&faces=7
• This information is available to PHP via the $_GET
variable.
- The action used to provide data to a PHP form influences the code
that we use to access it.
5
Example Using GET - HTML
<html>
<head>
<title>Dice Form</title>
</head>
<body>
<form action = "dice_roll_get.php" method = "get">
<p>How many dice</p>
<input type = "text" name = "num">
<p>How many faces?</p>
<input type = "text" name = "faces">
<input type = "submit" value = "Roll">
<input type = "reset" value = "Clear values">
</body>
</html>
6
Example Using GET - PHP
<?php
$num = $_GET["num"];
$faces = $_GET["faces"];
$total = 0;
$roll = 0;
7
Overview of GET
8
Why Use GET?
9
The POST Protocol
• The POST protocol is most useful on a day-to-day basis.
• POST has no limitations on size of data.
• It has no limitations on data types.
- You can use it to send binary data too.
• It works by placing the encoded data in a standard HTTP
header.
- As such, the data does not appear in the URL.
10
The Limitations of POST and GET
• Both of these protocols permit you to send data to a PHP
script.
• That data persists only as long as the script is running.
- If we reload a page that contains a script, it will usually ask if we
want to resend the data.
• If we move outside the confines of a single PHP script, we
will lose the data.
• That is a consequence of HTTP’s statelessness.
11
Cookies and Sessions - 1
12
Cookies
• When using cookies, we must declare them before any of
the HTML in a script.
- This is because they are part of an HTTP header rather than part
of the content.
13
Cookie Example
<?php
$thetext = $_POST["mytext"];
setcookie ("texttokeep", $thetext, time() + 10000);
?>
<html>
<head>
<title>Cookie Page</title>
</head>
<body>
<?
echo "<p>The post text " . $_POST["mytext"] .
", we won't be able to pass that on.</p>";
?>
</body>
</html> 14
The Next Page – cookie2.php
<html>
<head>
<title>Passed it on</title>
</head>
<body>
<?php
echo "<p>The post text is " . $_POST["mytext"] .
", we didn't get that passed on.</p>";
echo "<p>The text is still " . $_COOKIE["texttokeep"] .
", as we know from cookies.</p>";
?>
</body>
</html>
15
Manipulating Cookies
• We can change the value of a cookie by altering it directly
in the $_COOKIE variable:
- $_COOKIE[“texttokeep”] = “Hello World”;
16
Limitations of Cookies
17
Sessions - 1
18
Working with Sessions
<?php
session_start();
?>
19
Sessions - 2
20
Sessions Example
<?php
session_start();
?>
<html>
<head>
<title>Session Page</title>
</head>
<body>
<?php
$mytext = $_POST["mytext"];
</body>
</html>
21
Session_next_page.php
<?php
session_start();
?>
<html>
<head>
<title>Passed it on</title>
</head>
<body>
<?php
echo "<p>The session variable mytext is " .
$_SESSION["mytext"] . ".</p>";
?>
</body>
</html>
22
Manipulation of Sessions
• If you wish to delete session data, you can use the unset
function:
- unset ($_SESSION[“something_sensitive”]);
23
Cookies and Sessions - 2
• In the end, which you choose is based on several factors:
- Does the client accept cookies?
• If not, you will need sessions.
- Do you want to store user data over a significant period of time?
• If you do, you will need cookies.
24
Conclusion
25
Terminology
– Cookie
• A small piece of data stored on a user’s computer to ease
dynamic application development.
– Session
• A temporary mapping between the state of a server and a client’s
system.
26