Home Backend Development PHP Tutorial 抛开Cookie使用SESSION-PHP中SESSION不能跨页传递问题的解决途径_PHP

抛开Cookie使用SESSION-PHP中SESSION不能跨页传递问题的解决途径_PHP

Jun 01, 2016 pm 12:34 PM
cannot transfer use solve question

Cookie

抛开cookie使用session
PHP中SESSION不能跨页传递问题的解决办法

在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使用session,主要途径有三条:

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

通过例子来说明吧:

s1.php

session_start();
$_SESSION['var1']="中华人民共和国";
$url="下一页";
echo $url;
?>

s2.php

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

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

这也就是上面所说的途径1。

下面再说途径2:

修改的代码如下:
s1.php

session_start();
$_SESSION['var1']="中华人民共和国";
$sn = session_id();
$url="下一页";
echo $url;
?>

s2.php

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

办法3还是通过例子来说明:
login.html




Login



请登录:

用户名:

口 令:






mylogin1.php


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

?>

mylogin2.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 "请登录后浏览";
die();
}

?>

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

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

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


注:本人测试环境:Win2K Sever Aapache 1.3.31 PHP 4.3.4

另外,lidm在类unix系统上也测试通过

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to the problem that Win11 system cannot install Chinese language pack Solution to the problem that Win11 system cannot install Chinese language pack Mar 09, 2024 am 09:48 AM

Solution to the problem that Win11 system cannot install Chinese language pack

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

Five tips to teach you how to solve the problem of Black Shark phone not turning on!

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

See all articles