储存用户访问的全局唯一变量,存储在服务器上的php指定的目录中的
介绍:Session 是服务器端技术,一个会话对应一个Session文件,
通过php.ini文件来修改:
l 数据默认存放时间是1440秒, session.gc_maxlifetime=1440
l 存放路径session.save_path=”d:/tmp”
在使用session时需要在页面加入session_start()
另(不推荐):在php.ini文件中,session.auto_start=1;
Session(CRUD)增删改查
n 增--查
session_start(); //name:不能以数字开头 //values: 文件中可以保存"string,dobule,integer,bool,array,object" //bool为false时,打印为空。为true打印1
//string,integer,bool直接输出 $_SESSION['name']="this is mySession file"; $_SESSION['age']=13; $_SESSION['isOk']=true; echo $_SESSION['name']."<br />"; echo $_SESSION['age']."<br />"; echo $_SESSION['isOk']."<br />";
//array输出 $arr = array("aa","bb",13); $_SESSION["arr"]=$arr; $arr = $_SESSION['arr']; foreach($arr as $key=>$values){ echo $key."----".$values."<br />"; } //object输出 class Pesson{ private $name; private $age; private $sex;
function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; }
function getName(){ return $this->name; } } $pesson = new Pesson("小东", '13', "女"); $_SESSION['pesson']=$pesson; //取出object $pess = $_SESSION['pesson']; echo "<br />同学姓名:".$pess->getName();
|
保存的数据格式
name|s:22:"thisis mySession file";age|i:13;isBoy|b:1;arr1|a:3:{i:0;s:6:"北京";
name :key
s:数据类型
22:数据大小
*:session 中可以保存的数据类型是:string,integer,double,bool,array,object
注意:session 文件中可以放入多个key<==>val对,key不能重复,val是基本数据类型
如果取出对象,则需要实现申明一下类的定义信息(可以封装一个类)
n 更新:就是覆盖
$_SESSION['name']="session file"; |
n 删除
unset($_SESSION['name']); //删除一个 session_destroy(); //删除所有 |
禁用cookie:
Ø 在每个超链接上添加一个PHPSESSID=sessionId eg:booid=sn004
同时在每个页面加入
if(isset($_GET['PHPSESSID'])){ session_id($_GET('PHPSESSID')); } session_start(); |
Ø 使用常量SID可在链接后面直接拼接 注意:js处理页面的跳转
<a href=’ShopProcess.php?bookid-sn001&bookname=小说&”.SID.”’>小说</a> |
Ø 启用session.use_trans_sid 指定是否启用透明SID支持,
Session.use_trans_sid=1 //php.ini
<?php
session_start();
header("Content_Type:text/html;charset=utf-8");
/***************
* create Session
*/
echo "<br />---------------create Session------------------<br />";
$_SESSION['name']="damys";
if(isset($_SESSION['name'])){
echo "session name:".$_SESSION['name']."<br />";
}
/***************
* delete Session
*/
echo "<br />---------------delete Session------------------<br />";
unset($_SESSION['name']); //one
if(!isset($_SESSION['name'])){
echo "delete session success<br />";
}else{
echo "delete session fail<br />";
}
//all delete Session
session_destroy(); //two
/***************
* save to OOP
*/
echo "<br />---------------save to OOP------------------<br />";
class Person
{
private $name;
private $age;
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
public function getName(){
return $this->name;
}
public function getAge(){
return $this->age;
}
}
//OOP save to Session
$person = new Person('aaa',"20");
$_SESSION['person']=$person;
//oop get Session
//other page and person.class
$per = $_SESSION["person"];
echo $per->getName()."<br />";
echo $per->getAge()."<br />";
/***************
* save to Array
*/
echo "<br />---------------save to Array------------------<br />";
$arr = array("AA",123,"BB");
$_SESSION['arr'] = $arr;
//get
$arr_sesson = $_SESSION['arr'];
foreach($arr_sesson as $value){
echo $value."<br />";
}
?>