1.购物车应用
MyHall.php
<?php //购物大厅 echo "<a href='ShopProcess.php?bookid=sn1&bookname=天龙八部'>天龙八部</a><br/>"; echo "<a href='ShopProcess.php?bookid=sn2&bookname=红楼梦'>红楼梦</a><br/>"; echo "<a href='ShopProcess.php?bookid=sn3&bookname=西游记'>西游记</a><br/>"; echo "<a href='ShopProcess.php?bookid=sn4&bookname=聊斋'>聊斋</a><br/>"; echo "<hr/>"; echo "<a href='ShowCart.php'>查看购买到的商品列表</a>";
ShopPrecess.php
<?php
//接收用户购买请求,把购物的信息保存到 session 中
$bookid = $_GET['bookid'];
$bookname = $_GET['bookname'];
/*var_dump($bookid); var_dump($bookname); die;*/
//保存到 session 中
session_start();
$_SESSION[$bookid] = $bookname;
//var_dump($_SESSION);die;
echo "<br/>购买商品成功";
echo "<br/><a href='MyHall.php'>返回购物大厅继续购买</a>";
ShowCart.php
<?php echo "<h1>购物车商品有</h1>"; session_start(); var_dump($_SESSION);die; foreach($_SESSION as $k=>$v) { echo "<br/>书号:--$k -- 书名 -- $v"; }
2.如果禁用 cookie
解决办法:
浏览器禁止不了 session . 用 session_id(); session_start()会判断是否有session_id 。
方案1:在每个页面添加session_id(),确保它是唯一
MyHall.php
<?php
//购物大厅
if(isset($_GET['PHPSESSID'])){
session_id($_GET['PHPSESSID']);
}
session_start();
$sid = session_id();//会话开始后,有一个 session_id();
//var_dump($sid);die;
echo "<a href='ShopProcess.php?bookid=sn1&bookname=天龙八部&PHPSESSID=$sid'>天龙八部</a><br/>";
echo "<a href='ShopProcess.php?bookid=sn2&bookname=红楼梦&PHPSESSID=$sid'>红楼梦</a><br/>";
echo "<a href='ShopProcess.php?bookid=sn3&bookname=西游记&PHPSESSID=$sid'>西游记</a><br/>";
echo "<a href='ShopProcess.php?bookid=sn4&bookname=聊斋&PHPSESSID=$sid'>聊斋</a><br/>";
echo "<hr/>";
echo "<a href='ShowCart.php?PHPSESSID=$sid'>查看购买到的商品列表</a>";
ShopProcess.php
<?php //接收用户购买请求,把购物的信息保存到 session 中 if(isset($_GET['PHPSESSID'])){ session_id($_GET['PHPSESSID']); } //保存到 session 中 session_start(); $sid = session_id();//获取 id $bookid = $_GET['bookid']; $bookname = $_GET['bookname']; $_SESSION[$bookid] = $bookname; //var_dump($_SESSION);die; echo "<br/>购买商品成功"; echo "<br/><a href='MyHall.php?PHPSESSID=$sid'>返回购物大厅继续购买</a>";
ShowCart.php
<?php
echo "<h1>购物车商品有</h1>";
//这里不能让服务器创建一个新的 session 文件
if(isset($_GET['PHPSESSID'])){
session_id($_GET['PHPSESSID']);
}
session_start();
foreach($_SESSION as $k=>$v)
{
echo "<br/>书号:--$k -- 书名 -- $v";
}
4. session 设置
session.name
用 session_name(); 函数输出
session 垃圾回收机制:
设置成 1/1 做实验,会回收那些时间已经过期的 session 文件。
<?php class FileSessionHandler { private $savePath; function open($savePath, $sessionName) { $this->savePath = $savePath; if (!is_dir($this->savePath)) { mkdir($this->savePath, 0777); } return true; } function close() { return true; } function read($id) { return (string)@file_get_contents("$this->savePath/wjh_$id"); } function write($id, $data) { return file_put_contents("$this->savePath/wjh_$id", $data) === false ? false : true; } function destroy($id) { $file = "$this->savePath/wjh_$id"; if (file_exists($file)) { unlink($file); } return true; } function gc($maxlifetime) { foreach (glob("$this->savePath/wjh_*") as $file) { if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { unlink($file); } } return true; } } $handler = new FileSessionHandler(); // session_set_save_handler 有6个回调函数。 session_set_save_handler( array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc') ); // 下面这行代码可以防止使用对象作为会话保存管理器时可能引发的非预期行为 register_shutdown_function('session_write_close'); session_start();