~~~
/**
* 需求:需要跳转页面。但由于使用location.href跳转相当于是新开启了一个会话,就找不到用户的session了
* 解决思路:
* 1、使用session_id。
session_id可以理解为每个会话session的标识符,我们用session_id来区别,同一个用户在同一个浏览器的跳转
* 2、这样,我们就要在,当前(test.php)页面使用session_id()函数来获取session_id,
带到location.href的url上面
* 3、其他(index.php)页面,先获取url上的session_id,这样就知道跳转过来的是哪个用户了。
* 然后开启session_start()函数,来获取该用户在session中存的信息了
*/
~~~
- 效果
>[info] 不要纠结于用get不安全,想安全你可以发ajax请求
![](https://box.kancloud.cn/b466fb271943d25204e883471f242fad_1068x592.png)
![](https://box.kancloud.cn/e004fd9e950c2f5e63103b692808ec7c_1138x315.png)
- 代码
test.php页面
~~~
echo "
";session_start(); //1、开启session
$sessId = session_id(); //2、获取当前的session_id。这样带到其他页面拿这个id去找session
$_SESSION['name'] = 'xigua'; //3、将数据存到session中
$_SESSION['age'] = 26;
var_dump($_SESSION);
?>
跳转按钮
var sessId = '<?php echo $sessId;?>'; //将2、生成的session_id,带到url上
console.log(sessId);
function myHref(){
window.location.href = 'http://localhost/test/index.php?id=' + sessId;
}
~~~
index.php页面
~~~
header("content-type:text/html;charset=utf-8"); //设置编码
echo "
";session_id($_GET['id']); //4、通过接收test页面传过来的session_id,取出该会话
session_start(); //5、启动session
echo "
";
var_dump($_SESSION); //6、就可以取出session了
~~~