Web Applications
Development
ITSE 3302
تطوير تطبيقات الشبكة العالمية
Dr. Moamar Elyazgi
PHP Language
Chapter 7
PHP
PHP ─ Session
7.1 Introduction
PHP session is used to store and pass information from one page to another
temporarily (until user close the website).
PHP session technique is widely used in shopping websites where we need to store
and pass cart information e.g. username, product code, product name, product price
etc from one page to another.
PHP session creates unique user id
for each browser to recognize the
user and avoid conflict between
multiple browsers. 3
7.1 Introduction
4
7.2 Starting a PHP Session
o Before you can store any information in session variables, you must first
start up the session.
o To begin a new session, simply call the PHP session_start() function.
o It will create a new session and generate a unique session ID for the user.
<?php // Starting session session_start(); ?>
o The session_start() function first checks to see if a session already exists by looking for
the presence of a session ID. If it finds one, i.e. if the session is already started, it sets up
the session variables and if doesn't, it starts a new session by creating a new session ID.
5
7.3 Storing and Accessing Session Data
<?php
o You can store all your session data as // Start the session
key-value pairs in the $_SESSION[] session_start();
?>
superglobal array. <!DOCTYPE html>
o The stored data can be accessed <html>
<body>
during lifetime of a session. <?php
o Consider the following script, which // Set session variables
$_SESSION["favcolor"] = "green";
creates a new session and registers $_SESSION["favanimal"] = "cat";
two session variables. echo "Session variables are set.";
?>
Now, let's create a new page called </body>
"demo_session1.php". In this page 6
</html>
7.4 Get PHP Session Variable Values
o Next, we create another page called "demo_session2.php". From this page,
we will access the session information we set on the first page
("demo_session1.php").
o Notice that session variables are not passed individually to each new page,
instead they are retrieved from the session we open at the beginning of each
page (session_start()).
o Also notice that all session variable values are stored in the global
$_SESSION variable: 7
7.4 Get PHP Session Variable Values
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body> 8
</html>
7.5 Modify a PHP Session Variable
To change a session variable, just overwrite it:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
9
</html>
7.6 Destroy a PHP Session
To remove all global <?php
session_start();
session variables and ?>
destroy the session, use <!DOCTYPE html>
session_unset() and <html>
<body>
session_destroy():
<?php
Note: Before destroying a // remove all session variables
session with the session_unset();
session_destroy() function, // destroy the session
you need to first recreate the session_destroy();
session environment if it is echo "All session variables are now removed, and
not already there using the the session is destroyed."
session_start() function, so ?>
that there is something to </body>
destroy. </html>
10
7.8 Cookie Vs. Session
Cookies are client-side files that contain user information
Sessions are server-side files which contain user information
Cookie ends depending on the lifetime you set for it
A session ends when a user closes his browser
You don't need to start cookie as it is stored in your local machine
In PHP, before using $_SESSION, you have to write session_start(); Likewise for other
languages
The official maximum cookie size is 4KB
Within-session you can store as much data as you like. The only limits you can reach
is the maximum memory a script can consume at one time, which is 128MB by
default
A cookie is not dependent on Session
A session is dependent on Cookie
There is no function named unsetcookie() 11
Session_destroy(); is used to destroy all registered data or to unset some
12