文字

传送会话ID

有两种方式用来传送会话 ID:

  • Cookies
  • URL 参数

会话模块支持这两种方式。 Cookie 方式相对好一些,但是用户可能在浏览器中关闭 Cookie,所以 第二种方案就是把会话 ID 直接并入到 URL 中,以保证会话 ID 的传送。

无需开发人员干预,PHP 就可以自动处理 URL 传送会话 ID 的场景。 如果你使用 PHP 4.2.0 之前的版本,那么需要使用 --enable-trans-sid 参数来构建 PHP, 然后启用 session.use_trans_sid 选项, PHP 将会自动在相关 URI 中包含会话 ID。

Note:

arg_separator.output php.ini 配置指令允许你自定义会话 ID 参数分隔符。 可以设定为 & 来保持和 XHTML 的一致性。

会话开始之后,可以使用 SID 常量。 如果客户端未提供会话 cookie,该常量的展开形式为 session_name=session_id, 反之,该常量为空字符串。因此,可以直接在 URL 中包含此常量的展开字符串而无需考虑会话 ID 的实际传送方式。

下例演示了如何在会话中注册变量以及如何使用 SID 常量正确的链接到另一页面。

Example #1 某单一用户的点击数

<?php

session_start
();

if (empty(
$_SESSION [ 'count' ])) {
   
$_SESSION [ 'count' ] =  1 ;
} else {
   
$_SESSION [ 'count' ]++;
}
?>

<p>
Hello visitor, you have seen this page <?php  echo  $_SESSION [ 'count' ];  ?> times.
</p>

<p>
To continue, <a href="nextpage.php?<?php  echo  htmlspecialchars ( SID );  ?>">click
here</a>.
</p>

可以使用 htmlspecialchars() 来打印 SID 常量的展开字符串以避免 XSS 相关的攻击。

如果使用 --enable-trans-sid 参数编译的 PHP, 上例中打印 SID 常量部分就可以省略。

Note:

非相对 URL 将被视为指向外部站点的链接, 从安全角度考虑,外站的链接 URL 中将 不包含 SID 常量,以避免将 SID 泄露到外部服务器的风险。

用户评论:

[#1] Anonymous [2010-03-16 22:23:32]

The first time a page is accessed, PHP doesn't yet know if the browser is accepting cookies or not, so after session_start() is called, SID will be non-empty, and the PHPSESSID gets inserted in all link URLs on that page that are properly using SID.

This has the consequence that if, for example, a search engine bot hits your home page first, all links that it sees on your home page will have the ugly PHPSESSID=... in them.

This appears to be the default behavior. A work-around is to turn on session.use_only_cookies, but then you lose session data for anyone who has their cookies turned off.

[#2] a dot nielsen at shikadi dot net [2009-02-09 18:49:55]

When you generate URLs yourself using the SID variable, it must come first in the URL parameter list.  For example, if SID expands to "mysession=123" then this does NOT work:

  file.php?var1=value&mysession=123

The SID must come first:

  file.php?mysession=123&var1=value

If the SID does not come first then session_start() does not recognise it and it creates a new session instead of loading the old one.

上一篇: 下一篇: