Detailed explanation of Session usage in php

http://www.3lian.com/edu/2014/09-28/169168.htmlOriginal address


Session declaration and use

  Session settings are different from cookies, which must be started first, and session_start() must be called in PHP. The syntax of the session_start() function is as follows:

  Bool session_start(void) //Create a Session, start a session, and initialize the Session

  Note: There cannot be any output before the session_start() function

When visiting the website for the first time, the Seesion_start() function will create a unique Session ID and automatically save the Session ID to the client cookie through the HTTP response header. At the same time, a file named with the Session ID is also created on the server side to save the session information of the user. When the same user visits the website again, the session ID saved in the cookie will be automatically carried over through the HTTP request header. At this time, the Session_start() function will not allocate a new session ID, but will Find the session file with the same name as the session ID in the hard disk of the server, read out the session information saved for the user before, and apply it in the current script to track the user. Session is used in the form of an array, such as: $_SESSION['session name']

  Register a session variable and read Session

  Using Session variables in PHP, in addition to starting, also go through the registration process. Registering and reading Session variables are done by accessing the $_SESSION array. Key names in the $_SESSION associative array have the same naming conventions as normal variables in PHP. The code to register the Session variable looks like this:

 code show as below  


<?php
//启动session的初始化
session_start();
//注册session变量,赋值为一个用户的名称
$_SESSION["username"]="skygao";
//注册session变量,赋值为一个用户的ID
$_SESSION["uid"]=1;
?>

  执行该脚本后,两个Session变量就会被保存在服务器端的某个文件中,该文件的位置是通过php.ini文件,在session.save_path属性指定的目录下。

  注销变量与销毁Session

  当使用完一个Session变量后,可以将其删除,当完成一个会话后,也可以将其销毁。如果用户退出Web系统,就需要为他提供一个注销的功能,把他的所有信息在服务器中销毁。销毁和当前Session有关的所有的资料,可以调用session_destroy()函数结束当前的会话,并清空会话中的所有资源。该函数的语法格式如下所示:

  bool session_destroy(void) //销毁和当前Session有关的所有资料

  该函数并不会释放和当前Session相关的变量,也不会删除保存在客户端Cookie中的Session

  ID。因为$_SESSION数组和自定义的数组在使用上是相同的,所以我们可以使用unset()函数来释放在Session中注册的单个变量。如下所示:

  unset($_SESSION['键名']);

  一定要注意,不要使用unset($_SESSION)删除整个$_SESSION数组,这样将不能再通过$_SESSION超全局数组注册变量了。但如果想把某个用户在Session中注册的所有变量都删除,可以直接将数组变量$_SESSION赋上一个空数组。如下所示:

  $_SESSION=array()

  PHP默认的Session是基于Cookie的,Session

  ID被服务器存储在客户端的Cookie中,所以在注销Session时也需要清除Cookie中保存的SessionID,而这就必须借助setCookie()函数完成。在PHP脚本中,可以通过调用session_name()函数获取Session名称。删除保存在客户端Cookie中的Session

  ID,代码如下所示:

 代码如下  
<?php
//判断Cookie中是否存在session ID
if(isset($_COOKIE[session_name()])){
    //删除包含Session ID的cookie,注意第四个参数一定要和php.ini设置的路径相同
    setcookie(session_name(),'',time()-3600,'/');
}
?>

  通过前面的介绍可以总结出,Session的注销过程共需要4个步骤。在下例中,提供完整的四个步骤代码,运行该脚本就可以关闭Session,并销毁与本次会话有关的所有资源。代码如下所示:

 代码如下  

<?php
//第一步:开启Session并初始化
session_start();
 
//第二部:删除所有Session的变量,也可以用unset($_SESSION[XXX])逐个删除
$_SESSION = array();
 
//第三部:如果使用基于Cookie的session,使用setCookkie()删除包含Session ID的cookie
if(isset($_COOKIE[session_name()])) {
    setCookie(session_name(), "", time()-42000, "/");
}
 
//第四部:最后彻底销毁session
session_destroy();
 
?>

  session的phpini配置选项

  php.ini文件和Session有关的几个常用配置选项:

  session.auto_start = 0 ; 在请求启动时初始化session

  session.cache_expire = 180 ; 设置缓存中的会话文档在 n 分钟后过时

  session.cookie_lifetime = 0 ; 设置按秒记的cookie的保存时间,相当于设置Session的过期时间,为0时表示直到浏览器被重启

  session.auto_start=1,这样就无需每次使用session之前都要调用session_start()不建议使用.但启用该选项也有一些限制,如果确实启用了 session.auto_start,则不能将对象放入会话中,因为类定义必须在启动会话之前加载以在会话中重建对象。

  session.cookie_path = / ; cookie的有效路径

  session.cookie_domain = ; cookie的有效域

  session.name = PHPSESSID; 用在cookie里的session的名字

  session.save_handler = files ; 用于保存/取回数据的控制方式

  session.save_path = /tmp ; 在 save_handler 设为文件时传给控制器的参数, 这是数据文件将保存的路径.

  session.use_cookies = 1 ; 是否使用cookies

  Session的垃圾自动回收机制

可以通过session_destroy()函数在页面中提供一个“退出”按钮,通过单击销毁本次会话。但如果用户没有单击退出按钮,而是直接关闭浏览器,或断网等情况,在服务器端保存的Session文件是不会删除的。虽然关闭浏览器,下次需要重新分配一个新的Session ID重新登录,但这只是因为在php.ini中的设置seesion.cookie_lifetime=0,来设定Session ID在客户端Cookie中的有效限期,以秒为单位指定了发送到浏览器的Cookie的生命周期。当系统赋予Session有效期限后不管浏览器是否开启,Session ID都会自动消失。而客户端Session ID消失服务器端保存的Session文件并没有被删除。所以没有被Sessoin ID引用的服务器端Session文件,就成为了“垃圾”。

服务器保存的Session文件就是一个普通文本文件,所以都会有文件修改时间。“垃圾回收程序”启动后就是根据Session文件的修改时间,将所有过期的Session文件全部删除。通过在php.ini中设置session.gc_maxlifetime选项来指定一个时间(单位:秒),例如设置该选项值为1440(24分钟)。“垃圾回收程序”就会在所有Session文件中排查,如果有修改时间距离当前系统时间大于1440秒的就将其删除。

“session垃圾回收程序”是怎样的启动机制呢?“垃圾回收程序”是在调用session_start()函数时启动的。而一个网站有多个脚本,没有脚本又都要使用session_start()函数开启会话,又会有很多个用户同时访问,这就很可能session_start()函数在1秒内被调用N次,而如果每次都会启动“session垃圾回收程序”,这样是很不合理的。可以通过php.ini文件中修改“session.gc_probability和session.gc_divisor”两个选项,设置启动垃圾回收程序的概率。会根据“session.gc_probability/session.gc_divisor”公示计算概率,例如选项session.gc_probability=1,而选项session.gc_divisor=100,这样的概率就是“1/100”,即session_start()函数被调用100次才会有一次可能启动“垃圾回收程序”。

  php.ini中相关的配置

  session.cookie_lifetime=0; 关闭浏览器相应的cookie文件即被删除

  session.gc_maxlifetime; 设置过期session时间,默认1440秒(24分钟)

  session.gc_probability/session.gc_divisor; 启动垃圾回收机制的概率(建议值为1/1000——5000)

  cookie禁用时通过URL传递session的ID

  使用Session跟踪一个用户,是通过在各个页面之间传递唯一的Session ID,并通过Session ID提取这个用户在服务器中保存的Session变量。常见的Session ID传送方法有以下两种。

  第一种方法是基于cookie的方式传递session ID,这种方式更优,但不总是可用, 因为用户在客户端可以屏蔽cokie;

  第二种方法是通过url参数进行传递,直接将session ID嵌入到URL中去。

在Session的实现中通常都是采用Cookie的方式,客户端保存的Session ID就是一个Cookie。当客户禁用Cookie时,Session ID就不能在Cookie中保存,也就不能在页面之间传递,此时Session失效。不过PHP5在Linux平台可以自动检查Cookie状态,如果客户端禁用它,则系统自动把Session ID附加到URL上传送。而使用Windows系统作为Web服务器则无此功能。

  在PHP中提出了跟踪Session的另一种机制,如果客户浏览器不支持Cookie,则PHP可以重写客户请求的URL,把Session ID添加到URL信息中。可以手动地在每个超链接的URL中都加上一个Session ID,但工作量比较大,不建议使用这种方法。如下所示:

 代码如下  

<?php
//开启session
session_start();
 
//在每个URL后面附加上参数,变量名为session_name()获取名称,值通过session_id()获取
echo '<a href="demo.php?'.session_name().'='.session_id().'">连接演示</a>';
?>
在使用Linux系统做服务器时,则在编辑PHP时如果使用了–enable-trans-sid配置选项,和运行时选项session.use_trans_sid都被激活,在客户端禁用Cookie时,相对URL将被自动修改为包含会话ID。如果没有这么配置,或者使用Windows系统作为服务器时,可以使用常量SID。该常量在会话启动时被定义,如果客户端没有发送适当的会话Cookie,则SID的格式为session_name=session_id,否则就为一个空字符串。因此可以无条件地将其嵌入到URL中去。在下例中使用两个脚本程序,演示了Session ID的传送方法。

<?php
session_start();
 
$_SESSION["username"]="admin";
 
echo "session ID:".session_id()."<br>";
 
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325439563&siteId=291194637