Session warehouse receiving and phpsession warehouse receiving

Source: Internet
Author: User

Session warehouse receiving and phpsession warehouse receiving

Session_set_save_handler (callback open, callback close, callback read, call write, callback destroy, callback gc)

 

Execution time and usage of each parameter

Callback Function

Description

Open ()

Run session_start (). The declaration of this function requires two parameters. session in ini. the save_path option value is passed to the first parameter, and the session name is automatically passed to the second parameter. If true is returned, the operation continues.

Close ()

This function does not require parameters. It is executed when the script is executed or session_write_close () and session_destroy () is called. That is, it is executed after all session operations are completed. If no processing is required, the system returns true.

Read ()

Run session_start () because the current session data is read and the $ _ SESSION variable is written when the session is started. You need to declare a parameter. The system will automatically pass the SessionId to this function to obtain the corresponding user data through sessionId, and return the current user data to write $ _ SESSION data.

Write ()

This function is executed when the script ends and the $ _ SESSION variable is assigned a value. You need to declare two parameters: sessionid and the serialized session information string. When you assign a value to the $ _ SESSION variable, you can use Sessionid to locate the storage location and write the information. If the storage is successful, you can return true to continue the operation.

 

Destroy ()

To run session_destroy, you must declare a parameter. The system automatically passes the sessionId to this function to remove the corresponding session information.

Gc ()

The garbage collection program is executed at startup. You need to declare a parameter.

The value of the session. gc_maxlifetime option is passed to the function. The user deletes the session information that exceeds this time, and returns true to continue to execute

 

Session receiving is to save the session information to the table. You need to set session. save_handler = user

 

Create a session table

Drop table if exists 'session ';

Create table 'session '(

'Sid 'char (32) not null,

'Update _ time' int (11) default NULL,

'Data' text,

Primary key ('sid ')

) ENGINE = InnoDB default charset = utf8;

Sessionid, Update Time, and format data

 

Then write the functional code:

<? Php

$ Link = mysql_connect ("127.0.0.1", "root", "root ");

Mysql_select_db ("session ");

Mysql_query ("set names utf8 ");

 

Function open ($ save_path, $ session_name ){

Return true;

}

 

Function close (){

Return true;

}

 

Function read ($ sid ){

/* Use sid to first query the information of the current user from the database */

$ SQL = "select * from session where sid = '$ sid '";

// Echo $ SQL;

$ Re = mysql_query ($ SQL );

/* If no result is returned, an empty string is returned to the $ _ SESSION variable */

If (! $ Result = mysql_fetch_array ($ re )){

Return "";

}

/* If data is returned */

 

Return $ result ["data"];

}

 

Function write ($ sid, $ data ){

/* Obtain the user's session information from the database before each write operation */

$ SQL = "select * from session where sid = '$ sid '";

$ Re = mysql_query ($ SQL );

$ Time = time ();

/* If the user information exists, modify it. If the user information does not exist, add a new row of data */

If ($ result = mysql_fetch_array ($ re )){

// Conditions

$ Sql1 = "update session set update_time = '$ time', data =' $ data' where sid = '$ sid '";

Mysql_query ($ sql1 );

} Else {

// The error message does not exist.

If (! Empty ($ data )){

$ Sql1 = "insert into session (sid, update_time, data) values ('$ sid', '$ time',' $ data ')";

$ Sth1 = mysql_query ($ sql1 );

}

}

Return true;

 

}

 

Function destroy ($ sid ){

// Use sessionid to delete records of the current user

$ SQL = "delete from session where sid = '$ sid '";

Mysql_query ($ SQL );

Return true;

}

 

Function gc ($ maxfiletime ){

// Use sessionid to delete records of the current user

$ SQL = "delete * from session where update_time <$ maxfiletime ";

Mysql_query ($ SQL );

Return true;

}

Session_set_save_handler ("open", "close", "read", "write", "destroy", "gc ");

Session_start ();

?>

Related Article

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.