SlideShare a Scribd company logo
PHP Sessions
Atli
      Introduction:
ExpertSessions are one of the simplest and more powerful tools in a web developers arsenal.
  ModThis tool is invaluable in dynamic web page development and it is one of those things
 2.5K+every developer needs to know how to use.
    P:This article explains the basics of PHP Sessions.
4,457
        Assumptions:
        Basic PHP knowledge is required (variables, arrays and such)
        HTML Forms.

        What are Sessions?
        Sessions are a way of storing data. When developing interactive web applications we
        often find ourselves in need of a safe place to put certain pieces of information, such as
        User ID's and names. Somewhere it won’t be lost every time the browser is refreshed
        or redirected. This is exactly what Sessions do. They store your data on the server, so
        you can access it at any time, from within any server-side script.
        To make this possible a file is created on the server, it is linked to a SessionID that is
        generated and sent to the browser as a cookie or through the URL as GET data.
        Then, any time your browser is refreshed/redirected the server-side code reads this
        SessionID and loads the information stored in the file on the server.

        Why would I use Sessions?
        There are endless possible uses for this tool. It is commonly used to keep track of user
        information, such as Usernames and UserID's.
        For example, if you take a look at the top of the Bytes page your are currently on. If you
        are logged in you will see a welcome message and some user controls. These fields
        will stay the same no matter where you go on the Bytes web. To make this possible,
        your user info must be stored somewhere safe, where the server-side script will be
        able to read it. This is the very reason Sessions exists, to make things like this
        possible.

        How do I use Sessions?
        Using Sessions in PHP is very simple. First of all, you need to tell your script that you
        are going to be using Sessions.
        This is done by invoking the start_session() function. This function will either create a
        new session or re-open an existing one. Because this function needs to send header
        data to your browser, it must be called before any output is sent.

        Once you have told your browser to use sessions, you can access your session data
        by calling the $_SESSION super-global. This is an array, that works pretty much like
        any other PHP array. You can add, edit, read and unset it's fields just like you would a
        normal array.
Php sessions

More Related Content

Similar to Php sessions (20)

PPTX
lecture 13.pptx
ITNet
 
PDF
4 Basic PHP
Jalpesh Vasa
 
DOCX
It and ej
Harihar Kalia
 
PPTX
StateManagementintPHPStateManagementinPHP.pptx
sneha852132
 
PDF
PHP Unit-1 Introduction to PHP
Lariya Minhaz
 
PDF
PHP Basics
Roohul Amin
 
PPTX
PHP tutorials , php tutorials for beginners , tutorials for php
aimaq9a
 
PDF
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
ODP
Authentication and Single Sing on
guest648519
 
PPTX
Cookies and Session
KoraStats
 
PPT
Lecture8 php page control by okello erick
okelloerick
 
PPTX
My Saminar On Php
Arjun Kumawat
 
PPT
Manish
Manish Jain
 
DOCX
CONTENT MANAGEMENT SYSTEM
ANAND PRAKASH
 
PPTX
Intro to advanced web development
Stevie T
 
PDF
main report on restaurant
Neeraj Kumar
 
PPTX
Php
Yesha kapadia
 
PPTX
PHP Training In Ambala! BATRA COMPUTER CENTRE
jatin batra
 
PDF
Php Interview Questions
UmeshSingh159
 
lecture 13.pptx
ITNet
 
4 Basic PHP
Jalpesh Vasa
 
It and ej
Harihar Kalia
 
StateManagementintPHPStateManagementinPHP.pptx
sneha852132
 
PHP Unit-1 Introduction to PHP
Lariya Minhaz
 
PHP Basics
Roohul Amin
 
PHP tutorials , php tutorials for beginners , tutorials for php
aimaq9a
 
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
Authentication and Single Sing on
guest648519
 
Cookies and Session
KoraStats
 
Lecture8 php page control by okello erick
okelloerick
 
My Saminar On Php
Arjun Kumawat
 
Manish
Manish Jain
 
CONTENT MANAGEMENT SYSTEM
ANAND PRAKASH
 
Intro to advanced web development
Stevie T
 
main report on restaurant
Neeraj Kumar
 
PHP Training In Ambala! BATRA COMPUTER CENTRE
jatin batra
 
Php Interview Questions
UmeshSingh159
 

Recently uploaded (20)

PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Ad

Php sessions

  • 2. Atli Introduction: ExpertSessions are one of the simplest and more powerful tools in a web developers arsenal. ModThis tool is invaluable in dynamic web page development and it is one of those things 2.5K+every developer needs to know how to use. P:This article explains the basics of PHP Sessions. 4,457 Assumptions: Basic PHP knowledge is required (variables, arrays and such) HTML Forms. What are Sessions? Sessions are a way of storing data. When developing interactive web applications we often find ourselves in need of a safe place to put certain pieces of information, such as User ID's and names. Somewhere it won’t be lost every time the browser is refreshed or redirected. This is exactly what Sessions do. They store your data on the server, so you can access it at any time, from within any server-side script. To make this possible a file is created on the server, it is linked to a SessionID that is generated and sent to the browser as a cookie or through the URL as GET data. Then, any time your browser is refreshed/redirected the server-side code reads this SessionID and loads the information stored in the file on the server. Why would I use Sessions? There are endless possible uses for this tool. It is commonly used to keep track of user information, such as Usernames and UserID's. For example, if you take a look at the top of the Bytes page your are currently on. If you are logged in you will see a welcome message and some user controls. These fields will stay the same no matter where you go on the Bytes web. To make this possible, your user info must be stored somewhere safe, where the server-side script will be able to read it. This is the very reason Sessions exists, to make things like this possible. How do I use Sessions? Using Sessions in PHP is very simple. First of all, you need to tell your script that you are going to be using Sessions. This is done by invoking the start_session() function. This function will either create a new session or re-open an existing one. Because this function needs to send header data to your browser, it must be called before any output is sent. Once you have told your browser to use sessions, you can access your session data by calling the $_SESSION super-global. This is an array, that works pretty much like any other PHP array. You can add, edit, read and unset it's fields just like you would a normal array.