SlideShare a Scribd company logo
PHP Sessions
PHP Sessions
A PHP session variable is used to store information
about, or change settings for a user session.
Session variables hold information about one
single user, and are available to all pages in one
application.
PHP Session Variables
When you are working 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 and what you do
because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to
store user information on the server for later use (i.e.
username, shopping items, etc). However, session
information is temporary and will be deleted after the
user has left the website. If you need a permanent
storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each
visitor and store variables based on this UID. The UID
is either stored in a cookie or is propagated in the
URL.
Starting a PHP Session
Before you can store user information in your PHP
session, you must first start up the session.
Note: The session_start() function must appear
BEFORE the <html> tag:
<?php session_start(); ?>
<html>
<body>
</body>
</html>
The code above will register the user's session with the
server, allow you to start saving user information, and
assign a UID for that user's session.
Storing a Session Variable
The correct way to store and retrieve session variables is to use the
PHP $_SESSION variable:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
Output:
Pageviews=1
In the example below, we create a simple page-views
counter. The isset() function checks if the "views"
variable has already been set. If "views" has been set,
we can increment our counter. If "views" doesn't exist,
we create a "views" variable, and set it to 1:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
Destroying a Session
If you wish to delete some session data, you can
use the unset() or the session_destroy() function.
The unset() function is used to free the specified
session variable:
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>
You can also completely destroy the session by
calling the session_destroy() function:
<?php
session_destroy();
?>
Note: session_destroy() will reset your session and
you will lose all your stored session data.

More Related Content

PPTX
Sessions in php
PPTX
Topic 4 Session (1)jAhdgksjdhsjskshsjsksj.pptx
PPT
season management in php (WT)
PPTX
PHP SESSIONS & COOKIE.pptx
PPT
Session,cookies
DOC
Php sessions
PPT
Lecture8 php page control by okello erick
PPTX
StateManagementintPHPStateManagementinPHP.pptx
Sessions in php
Topic 4 Session (1)jAhdgksjdhsjskshsjsksj.pptx
season management in php (WT)
PHP SESSIONS & COOKIE.pptx
Session,cookies
Php sessions
Lecture8 php page control by okello erick
StateManagementintPHPStateManagementinPHP.pptx

Similar to lecture 13.pptx (20)

PPTX
PHP Cookies and Sessions
PPT
Manish
ODP
Session Management & Cookies In Php
ODP
PDF
PHP-Cookies-Sessions.pdf
PDF
4.4 PHP Session
PPSX
Php session
ODT
PPT
Php ssession - cookies -introduction
PDF
WEB Mod5@AzDOCUMENTS.in.pdf
PDF
WEB MODULE 5.pdf
PPTX
Php sessions
PPTX
PHP 2
PPSX
Sessions and cookies
PDF
Jsp session tracking
PDF
Cookies and sessions
PPTX
PHP COOKIES AND SESSIONS
PPTX
Session And Cookies In Servlets - Java
PHP Cookies and Sessions
Manish
Session Management & Cookies In Php
PHP-Cookies-Sessions.pdf
4.4 PHP Session
Php session
Php ssession - cookies -introduction
WEB Mod5@AzDOCUMENTS.in.pdf
WEB MODULE 5.pdf
Php sessions
PHP 2
Sessions and cookies
Jsp session tracking
Cookies and sessions
PHP COOKIES AND SESSIONS
Session And Cookies In Servlets - Java
Ad

More from ITNet (20)

PDF
lecture 8 b main memory
PPTX
lecture 9.pptx
PPTX
lecture 10.pptx
PPTX
lecture 11.pptx
PPTX
lecture 12.pptx
PPTX
lecture 15.pptx
PDF
kandegeeee.pdf
PPT
Ia 124 1621324160 ia_124_lecture_02
PPT
Ia 124 1621324143 ia_124_lecture_01
PPTX
Cp 121 lecture 01
PDF
Cp 111 5 week
DOCX
Teofilo kisanji university mbeya (TEKU) ambassador 2020
PPT
Tn 110 lecture 8
PPTX
Tn 110 lecture 2 logic
PPTX
Tn 110 lecture 1 logic
PPTX
internet
PPTX
Im 111 lecture 1
PPT
development study perspective full
PPTX
Gender issues in developement
PDF
Religion
lecture 8 b main memory
lecture 9.pptx
lecture 10.pptx
lecture 11.pptx
lecture 12.pptx
lecture 15.pptx
kandegeeee.pdf
Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324143 ia_124_lecture_01
Cp 121 lecture 01
Cp 111 5 week
Teofilo kisanji university mbeya (TEKU) ambassador 2020
Tn 110 lecture 8
Tn 110 lecture 2 logic
Tn 110 lecture 1 logic
internet
Im 111 lecture 1
development study perspective full
Gender issues in developement
Religion
Ad

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
August Patch Tuesday
PDF
Encapsulation theory and applications.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Tartificialntelligence_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Assigned Numbers - 2025 - Bluetooth® Document
August Patch Tuesday
Encapsulation theory and applications.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cloud_computing_Infrastucture_as_cloud_p
Digital-Transformation-Roadmap-for-Companies.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
A comparative study of natural language inference in Swahili using monolingua...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...

lecture 13.pptx

  • 2. PHP Sessions A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
  • 3. PHP Session Variables When you are working 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 and what you do because the HTTP address doesn't maintain state. A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database. Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.
  • 4. Starting a PHP Session Before you can store user information in your PHP session, you must first start up the session. Note: The session_start() function must appear BEFORE the <html> tag: <?php session_start(); ?> <html> <body> </body> </html> The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.
  • 5. Storing a Session Variable The correct way to store and retrieve session variables is to use the PHP $_SESSION variable: <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>
  • 7. In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?>
  • 8. Destroying a Session If you wish to delete some session data, you can use the unset() or the session_destroy() function. The unset() function is used to free the specified session variable: <?php session_start(); if(isset($_SESSION['views'])) unset($_SESSION['views']); ?>
  • 9. You can also completely destroy the session by calling the session_destroy() function: <?php session_destroy(); ?> Note: session_destroy() will reset your session and you will lose all your stored session data.