There are several ways to use the session
1. Cookies based
2. Based on the URL of the SID string
3. stored in the database
4. Stored in the memcache (the most efficient)
sessionhighsave.php
<?php/** * One: Session advanced conversation Control * 1. Resolve cross-Machine save session * in Linux using NF or Samba * using a database to implement * Memcache to save sessions * * Sess Ion_set_save_handler () * * 2. Statistics for online user information * * Two: Commonly used session configuration options * Session.name * Session.use_trans_sid in the use of cookies case, add all links to SessionID * Session.save_path Storage Path * * Session.use_cookies whether to use cookies * Session.cookie_path cookies to store bits * Session.cookie_domain Cookie's domain name * session.cookie_lifetime Cookie Survival Time * * GC: Garbage collection meaning * session.gc_maxlifetime unit is a second does not automatically clear, you need to specify the next two parameters * the following two are combined to start the garbage collector process management probability of using a * the probability of garbage collection at initialization (session_start) * Session.gc_probability=1 * SESSION.GC
_DIVISOR=100 * * Session.save_handler to control session storage: Files, memcache, user, etc. * * Third, using the Session.save_handler () function * */
Include ' sessionmm.class.php ';
can also be directly changed to file mode is as follows://include ' session.php ';
can also be directly changed to the database methods are as follows://include ' sessiondb.class.php ';
$_session["IsLogin"]=1;
$_session["username"]= "admin";
$_session["UID"]= "333"; echo "=================================". $_session["username"]; Echo Session_name (). session_id (). <br> ";?>
sessionhighget.php
<?php
include ' sessionmm.class.php ';
can also be directly changed to file mode is as follows:
//include ' session.php ';
can also be directly changed to the database methods are as follows:
//include ' sessiondb.class.php ';
echo "=================================";
Print_r ($_session);
echo "<br>";
Echo Session_name (). session_id (). <br> ";
? >
sessionhighdestroy.php
<?php
include ' sessionmm.class.php ';
can also be directly changed to file mode is as follows:
//include ' session.php ';
can also be directly changed to the database methods are as follows:
//include ' sessiondb.class.php ';
$_session = Array ();
if (Isset ($_cookie[session_name ()))
Setcookie (Session_name (), "", Time () -100, "/");
Session_destroy ();
Echo Session_name (). session_id (). <br> ";
? >
session.php
<?php/** * Execute/function open ($save _path, $session _name) {echo open () <br> when running the * Session_Start * method
;";
Global $sess _save_path;
$sess _save_path = $save _path; echo $save _path. "
: $session _name <br> ";
return true; /** * Execute/function close () {echo close () <br> when running the * session_write_close () * Session_destroy () * method
;";
return true; /** * Execute (read session data to $_session) when running the * session_start * $_session * * * * function Read ($id) {echo "read ()
<br> ";
Global $sess _save_path; $sess _file = $sess _save_path. "
/sid_ ". $id;
Return (String) @file_get_contents ($sess _file); /** * Execute/function write ($id, $sess _data) {echo write () when the Session_write_close () * method forces the submission of sesssion data at the end of the run
<br> ";
Global $sess _save_path; $sess _file = $sess _save_path. "
/sid_ ". $id;
if ($fp = @fopen ($sess _file, "W")) {$return = Fwrite ($fp, $sess _data);
Fclose ($FP);
return $return;
}else {return false; }
}
/**
* Execute/function Destroy ($ID) {echo destroy () <br>) at the end of Operation * Session_destroy () * method;
Global $sess _save_path; $sess _file = $sess _save_path. "
/sid_ ". $id;
Return @unlink ($sess _file); * Open, read, Session_Start will also perform GC/funct/** * session.gc_probability=1 * session.gc_divisor=100 * Properties
Ion GC ($maxlifetime) {echo "GC () <br>";
Global $sess _save_path; foreach (Glob ($sess _save_path.)
/sid_* ") as $filename) {if (Fileatime ($filename) + $maxlifetime) <time ()) {@unlink ($filename);
}
}; //Using this function, you need the Session.save_handler=user Session_set_save_handler in the INI ("open", "close", "read", "write", "Destroy", "GC"
);
Session_Start (); Session_destroy ();?>
sessiondb.class.php
<?php class session{private static $handler = null;
private static $IP = null;
private static $lifetime = null;
private static $time = null;
private static function init ($handler) {self:: $handler = $handler;
Self:: $ip =!empty ($_session["REMOTE_ADDR"])? $_session["REMOTE_ADDR"]: "Unknow";
Self:: $lifetime = Ini_get ("Session.gc_maxlifetime");
Self:: $time = time ();
static function Start (PDO $pdo) {self::init ($PDO); If it is object-oriented, you need to pass the array arguments, the first argument is the class name, the second is the method Session_set_save_handler (//__class__ represents this class, and the code array should be modified to prevent future class names from changing) (__class__, "Open"), Array (__class__, "close"), Array (__class__, "read"), Array (__class__, "write"), Array (__class__,
"Destroy"), Array (__class__, "GC"));
Session_Start ();
The public static function select ($PHPSESSID) {$sql = ' select * from sessiondb where phpsessid =? ';
$stmt = self:: $handler->prepare ($sql);
$stmt->execute (Array ($PHPSESSID));
return $stmt; } public static FunctioN Open ($save _path, $session _name) {return true;
The public static function close () {true; /** * Executes on the * Session_Start * $_session * Method (reads session data to $_session) */public static function read (
$PHPSESSID) {$stmt = Self::select ($PHPSESSID); if (!) (
$result = $stmt->fetch (PDO::FETCH_ASSOC))) {return '; if (self:: $ip!= $result ["Client_ip"]| | (($result ["Update_time"]+self:: $lifetime) <self:: $time))
{self::d Estroy ($PHPSESSID);
Return ";
return $result [' data ']; /** * Executes/public static function write ($PHPSESSID $d when the * Session_write_close () * method forces the submission of the sesssion data at the end of the run.
ATA) {$stmt = Self::select ($PHPSESSID); if ($result = $stmt->fetch (PDO::FETCH_ASSOC)) {///control 30 seconds to update if ($result [' Data ']!= $data | | Self:: $time > ($result ["Update_time"]+30)) {$sql = "update sessiondb set update_time=?,data=?"
where phpsessid=? ";
$stm = self:: $handler->prepare ($sql); $stm->execute Array (selF:: $time, $data, $PHPSESSID));
Return "; }}else{if (!empty ($data)) {$sql = INSERT into sessiondb (phpsessid,update_time,client_ip,data) VALUES (?,?,?,
?)";
$sth = self:: $handler->prepare ($sql);
$sth->execute ($PHPSESSID, Self:: $time, Self:: $ip, $data));
} return true; /** * Execute/public static function Destroy ($PHPSESSID) {$sql = "delet when the Session_destroy () * method is at the end of the run
E from sessiondb where PHPSESSID =? ";
$stmt = self:: $handler->prepare ($sql);
$stmt->execute (Array ($PHPSESSID));
return true;
/** * Session.gc_probability=1 * SESSION.GC_DIVISOR=100 * properties are jointly determined * open, read, Session_Start also execute GC/
private static function GC ($maxlifetime) {$sql = "delete from sessiondb where Update_time <";
$stmt = self:: $handler->prepare ($sql);
$stmt->execute (Array (self:: $time-$maxlifetime));
return true; The try {$pdo = new PDO ("Mysql:host=localhost;dbname=hibernaTe "," Root "," root ");
}catch (pdoexception $e) {echo $e->getmessage (); } session::start ($PDO);?>
sessionmm.class.php
<?php/** * If you do not want to write a class, you can modify * Session.save_handler=memcache with configuration files;
* session.save_path= "tcp://localhost:11211", "tcp:192.168.1.123:11211";
* This can be the fastest way to modify, but not easy to control */class memsession{private static $handler = null;
private static $IP = null;
private static $lifetime = null;
private static $time = null;
Specify different prefixes for different items const MS = "Session_";
private static function init ($handler) {self:: $handler = $handler;
Self:: $ip =!empty ($_session["REMOTE_ADDR"])? $_session["REMOTE_ADDR"]: "Unknow";
Self:: $lifetime = Ini_get ("Session.gc_maxlifetime");
Self:: $time = time ();
static function Start (Memcache $memcache) {self::init ($memcache); If it is object-oriented, you need to pass the array arguments, the first argument is the class name, the second is the method Session_set_save_handler (//__class__ represents this class, and the code array should be modified to prevent future class names from changing) (__class__, "Open"), Array (__class__, "close"), Array (__class__, "read"), Array (__class__, "write"), Array (__class__,
"Destroy"), Array (__class__, "GC"));
Session_Start (); Private StatiC function Session_key ($PHPSESSID) {return self::ms. $PHPSESSID;
The public static function select ($PHPSESSID) {$sql = ' select * from sessiondb where phpsessid =? ';
$stmt = self:: $handler->prepare ($sql);
$stmt->execute (Array ($PHPSESSID));
return $stmt;
public static function open ($save _path, $session _name) {return true;
The public static function close () {true; /** * Executes on the * Session_Start * $_session * Method (reads session data to $_session) */public static function read (
$PHPSESSID) {$out = self:: $handler->get (Self::session_key ($PHPSESSID)); if ($out ==false| |
$out ==null) return ";
return $out; /** * Executes/public static function write ($PHPSESSID $d when the * Session_write_close () * method forces the submission of the sesssion data at the end of the run. ATA) {$method = $data? '
Set ': ' Replace ';
Return self:: $handler-> $method (Self::session_key ($PHPSESSID), $data, memcache_compressed,self:: $lifetime);
/** * Executes at end of Run * Session_destroy () * method */public static function Destroy ($PHPSESSID) {return self:: $handler->delete (Self::session_key ($PHPSESSID));
/** * Session.gc_probability=1 * SESSION.GC_DIVISOR=100 * properties are jointly determined * open, read, Session_Start also execute GC/
The time is set in the private static function GC ($maxlifetime) {//memcached, so there is no need to implement the GC method for return true;
}} $memcache = new Memcache;
$memcache->connect ("localhost", 11211) or die ("could not Connect"); Memsession::start ($memcache);?>
memcache.php (used to query for stored data in Memcache server)
<?php
/**
* Get all memcached data
* Sometimes not found out, not clear the reason * *
$mem = new Memcache;
$host = "localhost";
$post = 11211;
$mem->connect ($host, $post);
$items = $mem->getextendedstats ("items");
$items = $items ["$host: $post"] ["items"];
foreach ($items as $value) {
foreach ($value as $item) {
$number = $item [' number '];
$arr = $mem->getextendedstats ("Cachedump", $number, 0);
$line = $arr ["$host: $post"];
if (Is_array ($line) &&count ($line) >0) {
foreach ($line as $key => $value) {
echo $key. " => ";
Print_r ($mem->get ($key));
echo "<br>";}}
? >