Sunteți pe pagina 1din 11

PHP SESSION

Prepared By: Prof. Disha H. Parekh


PHP SESSION
• A session is a way to store information (in variables) to be used across
multiple pages.
• Unlike a cookie, the information is not stored on the users computer.
• When you work with an application, you open it, do some changes, and
then you close it.
• This is much like a Session.
• The computer knows who you are.
• It knows when you start the application and when you end.
• But on the internet there is one problem: the web server does not know
who you are or what you do, because the HTTP address doesn't
maintain state.
PHP SESSION
• A session is a global variable stored on the server.
• Each session is assigned a unique id which is used to retrieve
stored values.
• Whenever a session is created, a cookie containing the unique
session id is stored on the user’s computer and returned with
every request to the server.  If the client browser does not
support cookies, the unique php session id is displayed in the
URL
• Sessions have the capacity to store relatively large data
compared to cookies.
PHP SESSION
• The session values are automatically deleted when the browser
is closed. If you want to store the values permanently, then you
should store them in the database.
• Just like the $_COOKIE array variable, session variables are
stored in the $_SESSION array variable. Just like cookies, the
session must be started before any HTML tags.
WHEN TO USE SESSION
• You want to store important information such as the user id more
securely on the server where malicious users cannot temper with
them.
• You want to pass values from one page to another.
• You want the alternative to cookies on browsers that do not
support cookies.
• You want to store global variables in an efficient and more
secure way compared to passing them in the URL
• You are developing an application such as a shopping cart that
has to temporary store information with a capacity larger than
4KB.
STARTING A PHP SESSION
• In order to  create a session, you must first call the PHP
session_start function and then store your values in the
$_SESSION array variable.
•  Let’s suppose we want to know the number of times that a page
has been loaded, we can use a session to do that.
• The code below shows how to create and retrieve values from
sessions
STARTING A PHP SESSION – SIMPLE EXAMPLE
• <?php
session_start(); //start the PHP_session function
?>
<html>
<body>
<?php
$_SESSION[“FName”] = “ABC”;
$_SESSION[“Lname”] = “XYZ”;
echo “First Name is: “ .$_SESSION[“FName”] . “<br>”;
echo “Last Name is: “ .$_SESSION[“LName”] . “<br>”;
?>
</body> </html>
STARTING A PHP SESSION
• <?php
session_start(); //start the PHP_session function
if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{
$_SESSION['page_count'] = 1;
}
echo 'You are visitor number ' . $_SESSION['page_count'];
?>
DESTROYING A SESSION VARIABLE
• The session_destroy() function is used to destroy the whole Php
session variables.
• If you want to destroy only a session single item, you use the
unset() function.
• The code below illustrates how to use both methods.
DESTROYING A SESSION VARIABLE
<?php
session_destroy();
//destroy entire session
?>
Another option is unset:
<?php
unset($_SESSION['product_item']);
//destroy product session item
?>
DESTROYING A SESSION VARIABLE
•  Session_destroy removes all the session data including cookies
associated with the session.
• Unset only frees the individual session variables.
• Other data remains intact.

S-ar putea să vă placă și