您好,登錄后才能下訂單哦!
一、禁用cookie緩存后如何使用session:
1、方法1
page1.php
<?php
//phpinfo();
session_start();
$_SESSION['var1']="aaaa";
$sn = session_id();
$url="<a href='page2.php?s=".$sn."'>next</a>";
echo $url;
?>
page2.php
<?php
session_id($_GET['s']);
session_start();
echo "var1:".$_SESSION['var1'];
?>
2、方法2
page1.php
<?php
session_start();
$_SESSION['var1']="aaaa";
$url="<a href='page2.php?".SID."'>next</a>"; //SID禁用cookie才會出現
echo $url;
?>
page2.php
<?php
session_id($_GET['PHPSESSID']);
session_start();
echo "var1:".$_SESSION['var1'];
?>
3、方法3
session.use_cookies = 0 //設置客戶端是否使用cookie來保存session值 該參數的值不影響上述機制的進行。
session.use_only_cookies = 0 //是否只使用cookie來保存session值 該參數為1時,上述機制失效。
session.use_trans_sid = 1
page1.php
<?php
session_start();
$_SESSION['var1']="aaaa";
$url="<a href='page2.php'>next</a>";
echo $url;
?>
page2.php
<?php
session_start();
echo "var1:".$_SESSION['var1'];
?>
二、自定義方式保存session到文件 (順序執行file1.php,file2.php,file2.php理解即可)
備注操作前請在php.ini文件中將session.save_handler = user(默認是files),并確保session.auto_start = 0
1、session.php
<?php
/**
* 自定義方式保存session到文件
* 使用session_set_save_handler 函數
* php.ini 設置
* 默認為session.save_handler = files,必須將其修改為session.save_handler = user,即變為用戶自定義方式。
*/
//在運行session_start()會執行
function open($save_path, $session_name) {//啟動回話
global $sess_save_path;
$sess_save_path=$save_path;
return true;
}
//sessin_writr_close() session_destroy()
function close() {
return true;
}
//session_start() ,$_SESSION
function read($id) {//讀取session
global $sess_save_path;
$sess_file=$sess_save_path."/mingc_".$id;
if (file_exists($sess_file)){
return (string)file_get_contents($sess_file);
}else {
return false;
}
}
//結束時和sessin_writr_close()強制提交數據是$_SESSION[]="aaa";
function write($id ,$sess_data) {
global $sess_save_path;
$sess_file=$sess_save_path."/mingc_".$id;
if ($fp=fopen($sess_file, "w")){
$return=fwrite($fp, $sess_data);
fclose($fp);
return $return;
}else {
return false;
}
}
//session_destroy()
function destroy($id) {
global $sess_save_path;
$sess_file=$sess_save_path."/mingc_".$id;
return unlink($sess_file);
}
//session.gc_probability和 session.gc_divisor值決定的,open(), read() session_start也會執行gc
function gc($maxlifetime) {
global $sess_save_path;
foreach (glob($sess_save_path."/mingc_*") as $filename){
if (filemtime($filename)+$maxlifetime < time()){
unlink($filename);
}
}
return true;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
?>
2、file1.php
<?php
//原始方式
/*
session_start();
$_SESSION["username"] = "admin";
$_SESSION["passwd"] = 123456;
echo session_name().'='.session_id()."<br>";
*/
//session_set_save_handler 方式
include 'session.php';
$_SESSION["username"] = "admin";
$_SESSION["passwd"] = 123456;
echo session_name().'='.session_id()."<br>";
?>
3、file2.php
<?php
//原始方式
/*
session_start();
print_r($_SESSION);
echo "<br>";
echo session_name().'='.session_id()."<br>";
*/
//session_set_save_handler 方式
include 'session.php';
print_r($_SESSION);
echo "<br>";
echo session_name().'='.session_id()."<br>";
?>
4、file3.php
<?php
//原始方式
/*
session_start();
$_SESSION = array();
if (isset($_COOKIE[session_name()])){
setcookie(session_name(), '' , time()-100, '/');
}
session_destroy();
echo session_name().'='.session_id()."<br>";
*/
//session_set_save_handler 方式
include 'session.php';
$_SESSION = array();
if (isset($_COOKIE[session_name()])){
setcookie(session_name(), '' , time()-100, '/');
}
session_destroy();
echo session_name().'='.session_id()."<br>";
?>
三、自定義方式保存session到數據庫
備注操作前請在php.ini文件中將session.save_handler = user(默認是files),并確保session.auto_start = 0
1、定義數據庫表
CREATE TABLE `Session` (
`Session_Id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Session_Expires` datetime NOT NULL,
`Session_Data` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`Session_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
2、inc.session.php
<?php
//inc.session.php
class SysSession implements SessionHandlerInterface
{
private $link;
public function open($savePath, $sessionName)
{
$link = mysqli_connect("127.0.0.1","root","root123456","session");
if($link){
$this->link = $link;
return true;
}else{
return false;
}
}
public function close()
{
mysqli_close($this->link);
return true;
}
public function read($id)
{
$result = mysqli_query($this->link,"SELECT Session_Data FROM Session WHERE Session_Id = '".$id."' AND Session_Expires > '".date('Y-m-d H:i:s')."'");
if($row = mysqli_fetch_assoc($result)){
return $row['Session_Data'];
}else{
return "";
}
}
public function write($id, $data)
{
$DateTime = date('Y-m-d H:i:s');
$NewDateTime = date('Y-m-d H:i:s',strtotime($DateTime.' + 1 hour'));
$result = mysqli_query($this->link,"REPLACE INTO Session SET Session_Id = '".$id."', Session_Expires = '".$NewDateTime."', Session_Data = '".$data."'");
if($result){
return true;
}else{
return false;
}
}
public function destroy($id)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE Session_Id ='".$id."'");
if($result){
return true;
}else{
return false;
}
}
public function gc($maxlifetime)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE ((UNIX_TIMESTAMP(Session_Expires) + ".$maxlifetime.") < ".$maxlifetime.")");
if($result){
return true;
}else{
return false;
}
}
}
date_default_timezone_set('PRC');
$handler = new SysSession();
session_set_save_handler($handler, true);
?>
3、page1.php
<?php
//page 1
require_once('inc.session.php');
session_start();
$_SESSION['var1'] = "hello world!";
?>
4、page2.php
<?php
require_once('inc.session.php');
session_start();
if(isset($_SESSION['var1'])){
echo $_SESSION['var1'];
}
?>
參考資料:
http://php.net/manual/zh/session.constants.php
http://php.net/manual/zh/function.session-set-save-handler.php
http://blog.csdn.net/masterft/article/details/1640122
http://blog.csdn.net/wzxsjdt/article/details/50508240
http://bbs.csdn.net/topics/340097849
http://blog.csdn.net/ysydao/article/details/53234347
http://www.jb51.net/article/57177.htm
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。