Session变量不能传送到下一页.解决: session.use_trans_sid = 1

在PHP中使用过SESSION的朋友可能会碰到这么一个问题,SESSION变量不能跨页传递。这令我苦恼了好些日子,最终通过查资料思考并解决了这个问题。我认为,出现这个问题的原因有以下几点:
1、客户端禁用了cookie
2、浏览器出现问题,暂时无法存取cookie
3、php.ini中的session.use_trans_sid = 0或者编译时没有打开--enable-trans-sid选项

为什么会这样呢?下面我解释一下:

Session 储存于服务器端(默认以文件方式存储session),根据客户端提供的session id来得到用户的文件,取得变量的值,session id可以使用客户端的Cookie或者Http1.1协议的Query_String(就是访问的URL的“?”后面的部分)来传送给服务器,然后服务器 读取Session的目录……。也就是说,session id是取得存储在服务上的session变量的身份证。当代码session_start();运行的时候,就在服务器上产生了一个session文件, 随之也产生了与之唯一对应的一个session id,定义session变量以一定形式存储在刚才产生的session文件中。通过session id,可以取出定义的变量。跨页后,为了使用session,你必须又执行session_start();将又会产生一个session文件,与之对应 产生相应的session id,用这个session id是取不出前面提到的第一个session文件中的变量的,因为这个session id不是打开它的“钥匙”。如果在session_start();之前加代码session_id($session id);将不产生新的session文件,直接读取与这个id对应的session文件。

PHP 中的session在默认情况下是使用客户端的Cookie来保存session id的,所以当客户端的cookie出现问题的时候就会影响session了。必须注意的是:session不一定必须依赖cookie,这也是 session相比cookie的高明之处。当客户端的Cookie被禁用或出现问题时,PHP会自动把session id附着在URL中,这样再通过session id就能跨页使用session变量了。但这种附着也是有一定条件的,即“php.ini中的session.use_trans_sid = 1或者编译时打开打开了--enable-trans-sid选项”。

用过论坛的朋友都知道,在进入论坛的时候,往往会 提示你检查Cookie是否打开,这是因为大多数论坛都是基于Cookie的,论坛用它来保存用户名、密码等用户信息,方便使用。而且很多朋友都认为 Cookie不安全(其实不是这样),往往禁用它。其实在PHP程序中,我们完全可以用SESSION来代替Cookie,它可以不依赖于客户端是否开启 Cookie。

所以,我们可以抛开cookie使用session,即假定用户关闭cookie的情况下使用session,其实现途径有以下几种:

1、设置php.ini中的session.use_trans_sid = 1或者编译时打开打开了--enable-trans-sid选项,让PHP自动跨页传递session id。
2、手动通过URL传值、隐藏表单传递session id。
3、用文件、数据库等形式保存session_id,在跨页过程中手动调用。

途径1举例说明:

s1.php

<?php
session_start();
$_SESSION[’var1’]="中华人民共和国";
$url="<a href=".""s2.php">下一页</a>";
echo $url;
?>

s2.php

<?php
session_start();
echo "传递的session变量var1的值为:".$_SESSION[’var1’];
?>

运行以上代码,在客户端cookie正常的情况下,应该可以在得到结果“中华人民共和国”。
现在你手动关闭客户端的cookie,再运行,可能得不到结果了吧。如果得不到结果,再“设置php.ini中的session.use_trans_sid = 1或者编译时打开打开了--enable-trans-sid选项”,又得到结果“中华人民共和国”

 

途径2举例说明:

s1.php

<?php
session_start();
$_SESSION[’var1’]="中华人民共和国";
$sn = session_id();
$url="<a href=".""s2.php?s=".$sn."">下一页</a>";
echo $url;
?>

s2.php

<?php
session_id($_GET[’s’]);
session_start();
echo "传递的session变量var1的值为:".$_SESSION[’var1’];
?>

隐藏表单的方法基本原理同上。

途径3举例说明:

login.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
请登录:
<form name="login" method="post" action="mylogin1.php">
用户名:<input type="text" name="name"><br>
口 令:<input type="password" name="pass"><br>
<input type="submit" value="登录">
</form>
</body>
</html>

mylogin1.php

<?php

$name=$_POST[’name’];
$pass=$_POST[’pass’];
if(!$name || !$pass) {
echo "用户名或密码为空,请<a href="login.html">重新登录</a>";
die();
}
if (!($name=="youngong" && $pass=="123") {
echo "用户名或密码不正确,请<a href="login.html">重新登录</a>";
die();
}
//注册用户
ob_start();
session_start();
$_SESSION[’user’]= $name;
$psid=session_id();
$fp=fopen("e:/tmp/phpsid.txt","w+";
fwrite($fp,$psid);
fclose($fp);
//身份验证成功,进行相关操作
echo "已登录<br>";
echo "<a href="mylogin2.php">下一页</a>";

?>

mylogin2.php

<?php
$fp=fopen("e:/tmp/phpsid.txt","r";
$sid=fread($fp,1024);
fclose($fp);
session_id($sid);
session_start();
if(isset($_SESSION[’user’]) && $_SESSION[’user’]="laogong" {

echo "已登录!";
}
else {
//成功登录进行相关操作
echo "未登录,无权访问";
echo "请<a href="login.html">登录</a>后浏览";
die();
}

?>

同样请关闭cookie测试,用户名:youngong 密码:123 这是通过文件保存session id的,文件是:e: mpphpsid.txt,请根据自己的系统决定文件名或路径。

至于用数据库的方法,我就不举例子了,与文件的方法类似。

总结一下,上面的方法有一个共同点,就是在前一页取得session id,然后想办法传递到下一页,在下一页的session_start();代码之前加代码session_id(传过来的session id);

==============================================================================

Thanks to all for their help on this. As a follow-up, and after a bunch of playing around with this yesterday,
here's what I've come to learn. Perhaps it will be helpful to others:

With enable-trans-id compiled into PHP and the following directives in php.ini:

session.use_cookies = 0 (PHP uses cookies for sessions - off)
session.use_trans_sid = 1 (PHP uses enable-trans-id for sessions - on)

PHP will automatically append the SID to the end of relative links 100% of the time and will not use cookies no
matter whether the user has cookies enabled for their browser or not.

In the following case (and I presume the more normal way of doing things):

session.use_cookies = 1 (PHP uses cookies for sessions - on)
session.use_trans_sid = 1 (PHP uses enable-trans-id for sessions - on)

PHP will behave the same way for those users that do *not* have cookies enabled for their browser as in the
first example, i.e. append links 100% of the time. However, for those users that have cookies enabled for their
browser, PHP will append the SID to the links only on the first hit to a page. Then, when a user requests the
next page, the auto-rewriting of the URI's stops and cookies are used from that point forward.

Actually, that all makes sense, as the first time a user requests a page, there's no way for PHP to know if the
browser will accept cookies or not. But, on the second request, the browser will send the cookie back to PHP
(along with the appended URI), and PHP from that point on knows that the browser accepts cookies and PHP will
then drop the rewriting of the URI's.

I hope I've got this all correct. The one observation I'd make in regards to using cookies vs. URI's to
maintain the session is this (and please someone correct me if I'm wrong):

If a user does *not* have cookies enabled for their browser, you can lose the session if the user hits an html
page at your site (because PHP will not be involved and will not rewrite the URI's for the .html page). Not
good.

If a user *does* have cookies enabled, they can hit non-PHP pages all they want and when they get back to a PHP
page, the session is still intact.

So, it would seem, while the SID being appended to all URI's should work for all users, non-PHP pages will break
the session (not good). And, as for the cookie method, not all users have cookies enabled for their browser
(also, not good). Therefore, IMO, neither the cookie method or appending the URI method will work as you'd like
100% of the time.

I suppose one thing you could do so that non-PHP pages won't break the session for those users that don't have
cookies enabled would be to just run every page in your site through PHP. That way, the URI's for every page
will be appended with the SID, and maybe that's the way to go.

posted @ 2014-05-19 18:52  土豆丫2013  阅读(270)  评论(0编辑  收藏  举报