Example of session loading in php

Source: Internet
Author: User

Example of a session warehouse receiving operation

The code is as follows: Copy code

<? Php
Ini_set ("session. save_handler", "user ");
// Session. gc_probability = 1 molecule
Ini_set ("session. gc_probability", 1 );
// Session. gc_divisor = 1000 denominator
Ini_set ("session. gc_divisor", 2 );
// Session. gc_maxlifetime = 1440 garbage collection time, session validity period
 
Session_set_save_handler ("open", "close", "read", "write", "destroy", "gc ");
 
// Connect to the database
Function open ($ savePath, $ sessionName ){
Mysql_connect ("localhost", "root", "root ");
Mysql_select_db ("test ");
Mysql_query ("set names utf8 ");
}
Function close (){
Echo "close <br> ";
}
Function read ($ sessionId ){
$ SQL = "select * from session where sessionid = '". $ sessionId ."'";
$ Re = mysql_query ($ SQL );
$ Sessdata = mysql_fetch_assoc ($ re );
     
// Return the data stored in the session
Return $ sessdata ["sessiondata"];
}
Function write ($ sessionId, $ data ){
     
// If the sessionid does not exist, insert a new record and update it if it exists.
/* $ SQL = "select * from session where sessionid = '". $ sessionId ."'";
$ Re = mysql_query ($ SQL );
If (mysql_num_rows ($ re)> 0 ){
$ SQL = "update session set sessiondata = '". $ data. "'Where sessionid ='". $ sessionId ."'";
} Else {
$ SQL = "insert into session (sessionid, sessiondata, sessiontime) values ('". $ sessionId. "','". $ data. "',". time (). ")";
     
    }
If (! Mysql_query ($ SQL )){
Echo mysql_error ();
} Else {
Return true;
    }
*/
     
// Use the replace syntax to solve the above operations
$ SQL = "replace into session (sessionid, sessiondata, sessiontime) values ('". $ sessionId. "','". $ data. "',". time (). ")";
If (! Mysql_query ($ SQL )){
Echo mysql_error ();
} Else {
Return true;
    }
 
}
 
// Destroy the current session according to sessionid
Function destroy ($ sessionId ){
Echo "destroy <br> ";
}
 
// Delete all expired sessions
Function gc ($ lifetime ){
Echo "gc <br> ";
}
 
 
Session_start ();
 
$ _ SESSION ["vvvv"] = "eeee ";
Var_dump ($ _ SESSION );
?>

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.