Loading video...
Introduction to Sessions
- On the web, a session refers to a series of related communications between a web client & web server
over a specific period of time that are used to:
- Link multiple HTTP transactions
- Preserve data over multiple transactions
- Data, or state, preserved between a series of related HTTP transactions is referred to as session data.
- A client-side session is when session data is stored by the web client, typically using cookies.
- A server-side session is when session data is stored by the web server, typically in a file, database, or in RAM.
- A unique session ID, or SID, is generated by a web server for each new server-side session.
- The SID is used by the server to:
- Identify multiple HTTP transactions as being part of the same session
- To access any data stored for the session
- A client passes an SID to the server with each HTTP request via either:
- Cookies
- URL Parameter
- PHP typically uses a cookie, known as a session cookie, to pass the SID of a session between the client & server with each HTTP request.
- PHP stores session data in flat files in the directory specified by the
session.save_pathconfiguration directive. session_start()either creates a new session or continues an existing session. If using cookie-based sessions, it must be called before any output is generated.$_SESSIONis a superglobal used to store all session data and can be used to store data of any type, including arrays and objects.- When a key/value pair is added to
$_SESSION, the key's name is referred to as a session variable. - To set, get, or delete session variables, standard associative array square bracket syntax & the
unset()construct are used on the$_SESSIONsuperglobal. - There are a number of configuration directives related to sessions in 'php.ini':
session.cookie_lifetime– sets the expiration of a session cookiesession.cookie_domain– sets the domain a session cookie is valid forsession.cookie_path– sets the path a session cookie is valid forsession.use_cookies– sets that cookies should be used to transfer SIDssession.use_trans_sid– sets that URL parameters should be used to transfer SIDssession.use_only_cookies– can be used to enforce the use of cookies for transferring SIDs
- Additional Resources:
Introduction to Sessions
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.
- Intro
- Lesson Overview
- What is a Session?
- Client-Side Sessions
- Server-Side Sessions
- Session IDs
- Session IDs
- The SID is Used by the Server to:
- A Client Passes an SID to the Server with Each HTTP Request Via:
- Sessions in PHP
- session_start()
- $_SESSION Superglobal
- Accessing Session Data
- Deleting Session Data
- Configuring PHP Sessions
- session.cookie_lifetime, session.cookie_domain, and session.cookie_path
- Coding Example: Configuring PHP Sessions
- Configuring PHP Sessions (cont.)
- session.use_cookies, session.use_trans_sid, and session.use_only_cookies
- Coding Example: Configuring PHP Sessions
- Homework Challenge
- Homework Challenge (cont.)
- Homework Challenge (cont.)

































Start Learning Now
Our free lessons will get you started (Flash® 10 required).
Sign up for Educator.comGet immediate access to our entire library.
Features Overview