SlideShare a Scribd company logo
1 of 6
PhP Sessions
Jigar Makhija
Introduction
• 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.
• Session variables solve this problem by storing user information to be
used across multiple pages
• (e.g. username, favorite color, etc). By default, session variables last
until the user closes the browser.
session_start() function
E.g.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
demo_session1.php
Get PHP Session Variable Values
create another page called "demo_session2.php". From this page,you will access the session information
that you will set on the first page ("demo_session1.php").
demo_session2.php
<?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"]
. ".";
?>
Modify a PHP Session Variable
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>
Destroy a PHP Session
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>

More Related Content

What's hot (20)

PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
PHP and Mysql
PHP and MysqlPHP and Mysql
PHP and Mysql
 
Php array
Php arrayPhp array
Php array
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Php introduction
Php introductionPhp introduction
Php introduction
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
PHP slides
PHP slidesPHP slides
PHP slides
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 

Similar to Php sessions

PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSDegu8
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptxITNet
 
PHP language presentation
PHP language presentationPHP language presentation
PHP language presentationAnnujj Agrawaal
 
Website Design, Development & Maintenance | Foundations | Week 01
Website Design, Development & Maintenance | Foundations | Week 01Website Design, Development & Maintenance | Foundations | Week 01
Website Design, Development & Maintenance | Foundations | Week 01Jason Fields
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxShitalGhotekar
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATIONkrutitrivedi
 
PHP tutorials , php tutorials for beginners , tutorials for php
PHP tutorials , php tutorials for beginners , tutorials for phpPHP tutorials , php tutorials for beginners , tutorials for php
PHP tutorials , php tutorials for beginners , tutorials for phpaimaq9a
 
php $_GET / $_POST / $_SESSION
php  $_GET / $_POST / $_SESSIONphp  $_GET / $_POST / $_SESSION
php $_GET / $_POST / $_SESSIONtumetr1
 
Deep Dive into FME Server 2016
Deep Dive into FME Server 2016 Deep Dive into FME Server 2016
Deep Dive into FME Server 2016 Safe Software
 
LAB PHP Consolidated.ppt
LAB PHP Consolidated.pptLAB PHP Consolidated.ppt
LAB PHP Consolidated.pptYasirAhmad80
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessoradeel990
 
Presentation on php and Javascript
Presentation on php and JavascriptPresentation on php and Javascript
Presentation on php and JavascriptPradip Shrestha
 
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...Ezra Gildesgame
 
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
AD113  Speed Up Your Applications w/ Nginx and PageSpeedAD113  Speed Up Your Applications w/ Nginx and PageSpeed
AD113 Speed Up Your Applications w/ Nginx and PageSpeededm00se
 
PHP Sessions and Non-Sessions
PHP Sessions and Non-SessionsPHP Sessions and Non-Sessions
PHP Sessions and Non-SessionsSven Rautenberg
 

Similar to Php sessions (20)

PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptx
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
PHP language presentation
PHP language presentationPHP language presentation
PHP language presentation
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Ph
PhPh
Ph
 
Website Design, Development & Maintenance | Foundations | Week 01
Website Design, Development & Maintenance | Foundations | Week 01Website Design, Development & Maintenance | Foundations | Week 01
Website Design, Development & Maintenance | Foundations | Week 01
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATION
 
PHP tutorials , php tutorials for beginners , tutorials for php
PHP tutorials , php tutorials for beginners , tutorials for phpPHP tutorials , php tutorials for beginners , tutorials for php
PHP tutorials , php tutorials for beginners , tutorials for php
 
php $_GET / $_POST / $_SESSION
php  $_GET / $_POST / $_SESSIONphp  $_GET / $_POST / $_SESSION
php $_GET / $_POST / $_SESSION
 
Deep Dive into FME Server 2016
Deep Dive into FME Server 2016 Deep Dive into FME Server 2016
Deep Dive into FME Server 2016
 
LAB PHP Consolidated.ppt
LAB PHP Consolidated.pptLAB PHP Consolidated.ppt
LAB PHP Consolidated.ppt
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
Presentation on php and Javascript
Presentation on php and JavascriptPresentation on php and Javascript
Presentation on php and Javascript
 
Lug
LugLug
Lug
 
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
 
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
AD113  Speed Up Your Applications w/ Nginx and PageSpeedAD113  Speed Up Your Applications w/ Nginx and PageSpeed
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
 
Php with mysql ppt
Php with mysql pptPhp with mysql ppt
Php with mysql ppt
 
PHP Sessions and Non-Sessions
PHP Sessions and Non-SessionsPHP Sessions and Non-Sessions
PHP Sessions and Non-Sessions
 

More from JIGAR MAKHIJA

Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matchingJIGAR MAKHIJA
 
Overview on Application protocols in Internet of Things
Overview on Application protocols in Internet of ThingsOverview on Application protocols in Internet of Things
Overview on Application protocols in Internet of ThingsJIGAR MAKHIJA
 
Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)JIGAR MAKHIJA
 
Embedded system lab work
Embedded system lab workEmbedded system lab work
Embedded system lab workJIGAR MAKHIJA
 
Presentation on iot- Internet of Things
Presentation on iot- Internet of ThingsPresentation on iot- Internet of Things
Presentation on iot- Internet of ThingsJIGAR MAKHIJA
 
Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120JIGAR MAKHIJA
 
View Alignment Techniques
View Alignment TechniquesView Alignment Techniques
View Alignment TechniquesJIGAR MAKHIJA
 
Letters (complaints & invitations)
Letters (complaints & invitations)Letters (complaints & invitations)
Letters (complaints & invitations)JIGAR MAKHIJA
 
Letter Writing invitation-letter
Letter Writing invitation-letterLetter Writing invitation-letter
Letter Writing invitation-letterJIGAR MAKHIJA
 
Communication skills Revised PPT
Communication skills Revised PPTCommunication skills Revised PPT
Communication skills Revised PPTJIGAR MAKHIJA
 
It tools &amp; technology
It tools &amp; technologyIt tools &amp; technology
It tools &amp; technologyJIGAR MAKHIJA
 
Communication skills
Communication skillsCommunication skills
Communication skillsJIGAR MAKHIJA
 

More from JIGAR MAKHIJA (20)

Php gd library
Php gd libraryPhp gd library
Php gd library
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matching
 
Db function
Db functionDb function
Db function
 
C++ version 1
C++  version 1C++  version 1
C++ version 1
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
SAP Ui5 content
SAP Ui5 contentSAP Ui5 content
SAP Ui5 content
 
Solution doc
Solution docSolution doc
Solution doc
 
Overview on Application protocols in Internet of Things
Overview on Application protocols in Internet of ThingsOverview on Application protocols in Internet of Things
Overview on Application protocols in Internet of Things
 
125 green iot
125 green iot125 green iot
125 green iot
 
Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)
 
Embedded system lab work
Embedded system lab workEmbedded system lab work
Embedded system lab work
 
Presentation on iot- Internet of Things
Presentation on iot- Internet of ThingsPresentation on iot- Internet of Things
Presentation on iot- Internet of Things
 
Oracle
OracleOracle
Oracle
 
Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120
 
View Alignment Techniques
View Alignment TechniquesView Alignment Techniques
View Alignment Techniques
 
Letters (complaints & invitations)
Letters (complaints & invitations)Letters (complaints & invitations)
Letters (complaints & invitations)
 
Letter Writing invitation-letter
Letter Writing invitation-letterLetter Writing invitation-letter
Letter Writing invitation-letter
 
Communication skills Revised PPT
Communication skills Revised PPTCommunication skills Revised PPT
Communication skills Revised PPT
 
It tools &amp; technology
It tools &amp; technologyIt tools &amp; technology
It tools &amp; technology
 
Communication skills
Communication skillsCommunication skills
Communication skills
 

Recently uploaded

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Recently uploaded (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 

Php sessions

  • 2. Introduction • 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. • Session variables solve this problem by storing user information to be used across multiple pages • (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.
  • 3. session_start() function E.g. <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?> </body> </html> demo_session1.php
  • 4. Get PHP Session Variable Values create another page called "demo_session2.php". From this page,you will access the session information that you will set on the first page ("demo_session1.php"). demo_session2.php <?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"] . "."; ?>
  • 5. Modify a PHP Session Variable <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // to change a session variable, just overwrite it $_SESSION["favcolor"] = "yellow"; print_r($_SESSION); ?> </body> </html>
  • 6. Destroy a PHP Session <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); ?> </body> </html>