A brief summary of PHP session _php Skills

Source: Internet
Author: User
Tags garbage collection php session sessions

The example of this article summarizes the operation skills of PHP session sessions. Share to everyone for your reference, specific as follows:

Session Technology

Session

Store session data on the server side while enabling session data to differentiate between browsers
Establishes a separate session data area for each session data (to store all the data for the current session), with a unique flag in each session data area, while the browser side stores the unique identity pairing.
The Session-id value given to the browser at the time of the response is also stored in the cookie data area on the browser side.

Ini:

Session.auto_start = 0

Session_Start ();

The use of $_session to check and delete is completed
Operation of the session data by manipulating the $_session array as you would an ordinary array of operations

Session principle

The session-id stored in the browser-side cookie is an ordinary cookie variable

Each session generates a database of sessions stored on the server side

The default is stored in file form in the Server system temp directory

Session.save_path = '/temp '

Session Data Properties

Validity period: A session cycle

Valid path: Whole station

Valid fields: Current domain

is only secure connection transfer: No

Whether HttpOnly: No

The characteristics of the session data above are caused by the characteristics of the session-id stored in the browser cookie. Visible if you need to change the properties of the session data, you need to change the properties of the cookie variable Phpsessid that stores Session-id:

PHP.ini there is a setting for this property:

Secure connection Transfer only:

; Http://php.net/session.cookie-secure
session.cookie_secure = n.

Life cycle:

; Lifetime in seconds of cookies or, if 0, until browser is restarted.
; Http://php.net/session.cookie-lifetime
session.cookie_lifetime = 0

Valid path:

; The path for which the cookies is valid.
; Http://php.net/session.cookie-path
Session.cookie_path =/

Valid fields:

; The domain for which the cookies is valid.
; Http://php.net/session.cookie-domain
Session.cookie_domain =

HttpOnly:

; Whether or not to add the HTTPONLY flag to the cookie which makes it inaccessible to browser scripting languages as JavaScript.
; Http://php.net/session.cookie-httponly
session.cookie_httponly =

If you need to modify the default properties, you can use the following methods:

1. Modify the php.ini configuration file;

2. Use Ini_set (key,val) in the script to set the configuration modification, only after the set script period is valid, must set up before Session_Start ();

3. Use Specific functional functions:

Session_set_cookie_params (validity period, valid path, valid domain, whether only secure transmission connection, whether HttpOnly);
Session_set_cookie_paramas (, '/', ' me.com ', true,true);
Session_Start ();

Session syntax Issues

The session data can be any type, because the data in the session data area is stored after serialization.

The subscript for $_session['] can only be a string

Output should not exist before session_start ()

Session Data Area

Persistent storage of current sessions session data outside of the scripting cycle

Use $_session to manage session data within the scripting cycle

Session Destroy

Deletes the session data area corresponding to the current conversation and closes the sessions mechanism (cannot be persisted after the end of the cycle)
Session_destroy ();
$_session still exists.

Empty session data

$_session = Array ();

All data for the current related session is deleted:

Session_destroy ();
Unset ($_session);
Setcookie (' Phpsessid ', "", Time ()-1);
Phpsessid called Session.name can get the current value by php.ini configuration
//Through Session_name ()
; Name of the session (used as cookie name).
; Http://php.net/session.name
session.name = Phpsessid

Overriding the session storage mechanism (inbound, into memory)

Objective:

Easy to manage large amounts of session data
Easy for Web server clusters to share session data

Realize:

Defining custom-related store-handling functions
Set it to the memory function required by the session mechanism (tells the session mechanism to use our function to complete the storage processing)

Session mechanism:

Session_set_save_handler ();

A total of six storage processing functions

Begin, end, read, write, Del, GC (garbage collection)

Needs to be used before session_start ()

<?php//session The earliest execution of a storage-related method used to initialize the associated resource function of the storage operation Ses_beg () {//echo ' begin</br> ';
 $link = mysql_connect (' 127.0.0.1 ', ' root ', ' root ');
 mysql_query (' Set names UTF8 ');
mysql_select_db (' session_override ');
 ///The method executed at the end of the session mechanism, and the last storage-related operation used to close the function ses_end () {echo ' end</br> ';
return true;
 The script automatically executes function ses_read ($ses _id) {//echo ' read</br> ') after the read, and does not need to be responsible for deserialization operations;
 $sql = "Select Session_content from session where session_id = ' $ses _id '";
 $res = mysql_query ($sql);
 if ($row = Mysql_fetch_assoc ($res)) {return $row [' session_content '];
 }else{return ";
 The script automatically executes the function ses_write ($ses _id, $ses _con) {//echo ' write</br> ';//Echo $ses _id, $ses _con, prior to writing;
 $sql = "Replace into the session values (' $ses _id ', ' $ses _con ', Unix_timestamp ())"; echo $sql. '
 </br> ';
return mysql_query ($sql);
 Perform function Ses_del ($ses _id) {//echo ' del</br> ' when calling Session_destroy ();
 $sql = "Delete from session where session_id = ' $ses _id '"; RetUrn mysql_query ($sql); //session.gc_maxlifetime = 1440//In the process of opening the session mechanism, the probability of executing the garbage collection mechanism//session.gc_probability = 1//session.gc_divisor =
 1000//@param session.gc_maxlifetime//return boolean function ses_gc ($maxlifetime) {//echo ' gc</br> ';
 Ini_set (' Session.gc_divisor ', 2);
 $sql = ' Delete from session_override where Last_time<unix_timestamp ()-'. $maxlifetime;
return mysql_query ($sql);
//filesphp built-in session storage Processor//rewrite session mechanism, should be changed to user custom Ini_set (' Session.save_handler ', ' username '); Configure the storage mechanism before start, make sure that the session does not automatically open//session.auto_start=0//.htacess php_flag session.auto_start 0 session_set_save_
Handler (' Ses_beg ', ' ses_end ', ' ses_read ', ' ses_write ', ' Ses_del ', ' ses_gc ');
Session_Start ();
$_session[' id ' = ' test ';

 Session_destroy ();

Session and Cookies

Contact

is the implementation of conversational technology
Session based on Cookies

Difference

Session
Tables Cookies
Session Data Storage Location Browser end Server-side
Security Low High
Amount of data transferred Big Small
Support Session Data Volume With a limit of 4K 20 No size limit
Support data types String All

Session Data Persistence

Browser-side Session-id

Session_set_params (3600)

Server-side session data area

Ini_set (' session.gc_maxlifetime ', ' 3600 ')

Cookie disabled

Under normal circumstances, cookies are disabled and session cannot be used

Technology can carry Session-id to the server via get or post mode

Whether to use cookies only to carry Session-id
ini_set (' session.use_only_cookies ', ' 0 ');
Whether automatic transmission of session-id by other means, unsafe
ini_set (' Session.use_trans_sid ', ' 1 ');

More interested in PHP related content readers can view the site topics: "PHP Cookie Usage Summary", "PHP Array" operation Techniques Daquan, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introduction Tutorial" , "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.