How Sessions Are Support in PHP?
This section describes how sessions are supported in PHP. Session IDs are passed as cookies or GET/POST variables. session_start() is the built-in function to start a session. $_SESSION is the built-in array to manage session data.
PHP supports Web sessions with following main elements in the configuration file, php.ini:
1. session.use_cookies = 1/0 - A setting to determine if the session ID should be
stored and managed as a cookie.
- session.use_cookies = 1 (the default setting) - Managing the session ID as a cookie.
The session ID is stored in a cookie with a default name of "PHPSESSID". This "PHPSESSID" cookie will be automatically inserted into HTTP responses
and retrieved from HTTP requests by the PHP engine.
- session.use_cookies = 0 - Stop storing the session ID as a cookie.
2. session.use_trans_sid = 1/0 - A setting in the configuration file, php.ini, to control if the session ID should be
stored and managed transparently as a parameter in the URL.
- session.use_trans_sid = 1 - Managing the session ID as an extra transparent parameter in the URL.
The extra URL parameter contains the session ID value with a default name of "PHPSESSID".
This "PHPSESSID" parameter will be automatically inserted into all links in the HTTP response,
and retrived from HTTP requests by the PHP engine.
- session.use_trans_sid = 0 (the default setting) - Stop storing the session ID as a URL transparent parameter.
3. session.save_handler = files/mm/user - A setting in the configuration file, php.ini, to control how session data
should be stored.
- session.save_handler = files (the default setting) - Storing session data as files in a directory defined in the
session.save_path = "/tmp" setting.
- session.save_handler = mm - Storing session data in shared memory.
- session.save_handler = user - Storing session data through user-defined functions.
4. session_start() - A built-in function to create a new session or resume an existing session.
When session_start() is called, the PHP engine will check the current HTTP request to see if an existing session ID
is included or not.
- If no session ID is found, the PHP engine will create a new session with a new session ID.
- If a session ID is found, the PHP engine will restore the session identified by this session ID.
If the restored session has been timed out, an error will be issued.
5. $_SESSION - A built-in array attached to the current session acting as a storage.
You can store any number of key-value pairs in $_SESSION in a PHP script and retrieve them later
in another script if it shares the same session as the first script.
6. session_name() - A built-in function to set and get the name of the current session.
7. session_id() - A built-in function to set and get the session ID of the current session.
8. session_destroy() - A built-in function to destroy the current session.
When the current session is destroyed, information stored in $_SESSION will be removed.
Last update: 2015.
Table of Contents
About This Book
Introduction and Installation of PHP 7.0
PHP Script File Syntax
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Arrays - Ordered Maps
Configuring and Sending out Emails
Retrieving Information from HTTP Requests
►Creating and Managing Sessions in PHP Scripts
What Is a Session?
►How Sessions Are Support in PHP?
SessionPage*.php - Session Test Script Pages
Running Session Test Script Pages
Managing Session IDs without Cookies
Where Is Session Data Stored?
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Introduction of Class and Object
Integrating PHP with Apache Web Server
PDF Printing Version