SlideShare a Scribd company logo
PHP Session [email_address]
Agenda What is session in PHP? Why use session? What are important special variables? How to apply session in your web application? Sample application.
What is Session in PHP? $_SESSION[‘variable’] A special type of variable whose value is maintained across subsequent pages Its value will be destroyed if The user closes the web browser, or the maximum time allotment set on the server for session lifetime is exceeded, or session_destroy()
Why Session? the HTTP protocol used to browse the web is stateless  Each request for a page is completely independent of earlier requests  so if you want subsequent pages to "remember" the user’s name that he/she entered on your front page, you have to store that information somewhere.
Important Special Variables $_SESSION[]  -  PHP super global array variable that contains currently registered to a script's session. session_start()  -  initialises session data. This function is called prior to creating a new session variable using $_SESSION[]. session_destroy()  - destroys all data registered to a script's current session. session_id()  - used to get the id value for the current session.
Redirecting to Another Page header("Location: http://www.domain.com")  - header function used redirect the browser page to the Location parameter provided.
Applying Session: register a session <?php if (isset($_POST['submit'])) { if ($_POST['username'] == 'admin' && $_POST['password'] == 'admin') { session_start();   $_SESSION['username'] = $_POST['username'];   header('Location:index.php'); } else echo &quot;You have entered wrong username and password&quot;; } else { echo &quot;Please enter valid username and password&quot;; } ?>
Applying Session: start session in subsequent pages <?php session_start(); if ( !isset($_SESSION['username'] )) { header('Location:login.php'); } else { echo &quot;<div align=\&quot;right\&quot;>&quot;; echo &quot;login as: &quot; . $_SESSION['username']; echo &quot; | <a href=\&quot;logout.php\&quot;>logout</a>&quot;; echo &quot;</div>&quot;; } ?>
Applying Session: Kill a Session <?php session_start(); session_destroy(); header('location:index.php'); ?>

More Related Content

What's hot (20)

PPT
season management in php (WT)
kunjan shah
 
DOC
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
PDF
Php login system with admin features evolt
GIMT
 
PDF
Php workshop L04 database
Mohammad Tahsin Alshalabi
 
PDF
Php workshop L03 superglobals
Mohammad Tahsin Alshalabi
 
PPT
Cookies and sessions
UdaAs PaNchi
 
PPT
17 sessions
Abhijit Gaikwad
 
PPT
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
PPTX
18.register login
Razvan Raducanu, PhD
 
PPT
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
PDF
Introduction to PHP
Bradley Holt
 
PPTX
Php talk
Jamil Ramsey
 
PDF
backend
tutorialsruby
 
PDF
Add loop shortcode
Peter Baylies
 
PPT
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
PDF
Check username availability with vue.js and PHP
Yogesh singh
 
PPT
Cookies and sessions
Lena Petsenchuk
 
PPT
PHP - Getting good with cookies
Firdaus Adib
 
season management in php (WT)
kunjan shah
 
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
Php login system with admin features evolt
GIMT
 
Php workshop L04 database
Mohammad Tahsin Alshalabi
 
Php workshop L03 superglobals
Mohammad Tahsin Alshalabi
 
Cookies and sessions
UdaAs PaNchi
 
17 sessions
Abhijit Gaikwad
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
18.register login
Razvan Raducanu, PhD
 
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
Introduction to PHP
Bradley Holt
 
Php talk
Jamil Ramsey
 
backend
tutorialsruby
 
Add loop shortcode
Peter Baylies
 
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
Check username availability with vue.js and PHP
Yogesh singh
 
Cookies and sessions
Lena Petsenchuk
 
PHP - Getting good with cookies
Firdaus Adib
 

Viewers also liked (11)

PPT
Php ssession - cookies -introduction
Programmer Blog
 
PPTX
Cookies in PHP
Ashok Kumar
 
PPTX
PHP Cookies and Sessions
Nisa Soomro
 
ODP
Session Management & Cookies In Php
Harit Kothari
 
PPT
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
PPSX
Php session
argusacademy
 
PPTX
Php string function
Ravi Bhadauria
 
PPSX
Sessions and cookies
www.netgains.org
 
PPTX
Cookie and session
Aashish Ghale
 
PPTX
Introduction to Web Architecture
Chamnap Chhorn
 
ODP
Session por nieves
Saregune Multimedia
 
Php ssession - cookies -introduction
Programmer Blog
 
Cookies in PHP
Ashok Kumar
 
PHP Cookies and Sessions
Nisa Soomro
 
Session Management & Cookies In Php
Harit Kothari
 
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
Php session
argusacademy
 
Php string function
Ravi Bhadauria
 
Sessions and cookies
www.netgains.org
 
Cookie and session
Aashish Ghale
 
Introduction to Web Architecture
Chamnap Chhorn
 
Session por nieves
Saregune Multimedia
 
Ad

Similar to Php - Getting good with session (20)

PPT
Parameter Passing & Session Tracking in PHP
amichoksi
 
PPTX
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
PDF
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
PPT
Php Sessoins N Cookies
mussawir20
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PPTX
Sessions and cookies in php
Pavan b
 
PDF
How to Create Login and Registration API in PHP.pdf
Appweb Coders
 
ODP
Exploring Symfony's Code
Wildan Maulana
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
ODP
Web Security
Rene Churchill
 
PPT
Php frameworks
Anil Kumar Panigrahi
 
PPT
Zend Con 2008 Slides
mkherlakian
 
TXT
Jsp Notes
Rajiv Gupta
 
PPT
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
PPT
Php My Sql Security 2007
Aung Khant
 
PPT
session.ppt
pshtiwanabasabdulla
 
PPT
Framework
Nguyen Linh
 
DOC
Php sessions
clickon2010
 
PPTX
RESTful Services for the Programmable Web with Windows Communication Foundation
goodfriday
 
Parameter Passing & Session Tracking in PHP
amichoksi
 
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
Php Sessoins N Cookies
mussawir20
 
Silex Cheat Sheet
Andréia Bohner
 
Silex Cheat Sheet
Andréia Bohner
 
Sessions and cookies in php
Pavan b
 
How to Create Login and Registration API in PHP.pdf
Appweb Coders
 
Exploring Symfony's Code
Wildan Maulana
 
Web Security
Rene Churchill
 
Php frameworks
Anil Kumar Panigrahi
 
Zend Con 2008 Slides
mkherlakian
 
Jsp Notes
Rajiv Gupta
 
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
Php My Sql Security 2007
Aung Khant
 
session.ppt
pshtiwanabasabdulla
 
Framework
Nguyen Linh
 
Php sessions
clickon2010
 
RESTful Services for the Programmable Web with Windows Communication Foundation
goodfriday
 
Ad

More from Firdaus Adib (20)

PDF
Rule Based Architecture System
Firdaus Adib
 
PDF
Wireless Technology Proj spec
Firdaus Adib
 
PDF
Corporate Ethics January 2010
Firdaus Adib
 
PDF
Corporate Ethics July 2008
Firdaus Adib
 
PDF
Final Paper UTP Web Development Application July 2008
Firdaus Adib
 
PDF
Final Paper UTP Algorithm Data Structure July 2008
Firdaus Adib
 
PDF
Final Paper UTP Web Development Application January 2010
Firdaus Adib
 
PDF
Final Paper UTP Algorithm Data Structure January 2010
Firdaus Adib
 
PDF
Final Paper UTP Web Development Application July 2009
Firdaus Adib
 
PPT
PHP - Getting good with MySQL part II
Firdaus Adib
 
PPT
PHP - Getting good with MySQL part I
Firdaus Adib
 
PPT
Javascript - Getting Good with Object
Firdaus Adib
 
PPT
Javascript - Getting Good with Loop and Array
Firdaus Adib
 
PPT
Introduction to Javascript
Firdaus Adib
 
DOCX
Additional exercise for apa references
Firdaus Adib
 
PPT
Chapter 2 summarising
Firdaus Adib
 
PPTX
American psychological association (apa)
Firdaus Adib
 
PPT
Referencing and Citing
Firdaus Adib
 
PPT
Chapter 2 paraphrasing
Firdaus Adib
 
PDF
Introduction to oil & gas [read only]
Firdaus Adib
 
Rule Based Architecture System
Firdaus Adib
 
Wireless Technology Proj spec
Firdaus Adib
 
Corporate Ethics January 2010
Firdaus Adib
 
Corporate Ethics July 2008
Firdaus Adib
 
Final Paper UTP Web Development Application July 2008
Firdaus Adib
 
Final Paper UTP Algorithm Data Structure July 2008
Firdaus Adib
 
Final Paper UTP Web Development Application January 2010
Firdaus Adib
 
Final Paper UTP Algorithm Data Structure January 2010
Firdaus Adib
 
Final Paper UTP Web Development Application July 2009
Firdaus Adib
 
PHP - Getting good with MySQL part II
Firdaus Adib
 
PHP - Getting good with MySQL part I
Firdaus Adib
 
Javascript - Getting Good with Object
Firdaus Adib
 
Javascript - Getting Good with Loop and Array
Firdaus Adib
 
Introduction to Javascript
Firdaus Adib
 
Additional exercise for apa references
Firdaus Adib
 
Chapter 2 summarising
Firdaus Adib
 
American psychological association (apa)
Firdaus Adib
 
Referencing and Citing
Firdaus Adib
 
Chapter 2 paraphrasing
Firdaus Adib
 
Introduction to oil & gas [read only]
Firdaus Adib
 

Recently uploaded (20)

PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
CapCut Pro PC Crack Latest Version Free Free
josanj305
 

Php - Getting good with session

  • 2. Agenda What is session in PHP? Why use session? What are important special variables? How to apply session in your web application? Sample application.
  • 3. What is Session in PHP? $_SESSION[‘variable’] A special type of variable whose value is maintained across subsequent pages Its value will be destroyed if The user closes the web browser, or the maximum time allotment set on the server for session lifetime is exceeded, or session_destroy()
  • 4. Why Session? the HTTP protocol used to browse the web is stateless Each request for a page is completely independent of earlier requests so if you want subsequent pages to &quot;remember&quot; the user’s name that he/she entered on your front page, you have to store that information somewhere.
  • 5. Important Special Variables $_SESSION[] - PHP super global array variable that contains currently registered to a script's session. session_start() - initialises session data. This function is called prior to creating a new session variable using $_SESSION[]. session_destroy() - destroys all data registered to a script's current session. session_id() - used to get the id value for the current session.
  • 6. Redirecting to Another Page header(&quot;Location: http://www.domain.com&quot;) - header function used redirect the browser page to the Location parameter provided.
  • 7. Applying Session: register a session <?php if (isset($_POST['submit'])) { if ($_POST['username'] == 'admin' && $_POST['password'] == 'admin') { session_start(); $_SESSION['username'] = $_POST['username']; header('Location:index.php'); } else echo &quot;You have entered wrong username and password&quot;; } else { echo &quot;Please enter valid username and password&quot;; } ?>
  • 8. Applying Session: start session in subsequent pages <?php session_start(); if ( !isset($_SESSION['username'] )) { header('Location:login.php'); } else { echo &quot;<div align=\&quot;right\&quot;>&quot;; echo &quot;login as: &quot; . $_SESSION['username']; echo &quot; | <a href=\&quot;logout.php\&quot;>logout</a>&quot;; echo &quot;</div>&quot;; } ?>
  • 9. Applying Session: Kill a Session <?php session_start(); session_destroy(); header('location:index.php'); ?>