简体   繁体   English

session_start不起作用:(

[英]session_start doesn't work :(

I start a new session with : session_start(); 我开始一个新的会话: session_start(); then I set some session variables like this : 然后我设置了一些这样的会话变量:

$_SESSION['name']=$_POST['name'];

and some another variables. 和另一些变量。

At bottom of page I set header to different page : 在页面底部我将标题设置为不同的页面:

header('location: index.php');
exit();

Now in new page (index.php) I can't access to my session variables, like $_SESSION['name'] . 现在在新页面(index.php)中,我无法访问我的会话变量,例如$_SESSION['name']

What's wrong ? 怎么了 ?

Thanks. 谢谢。

Are you calling session_start() in your other pages where you're trying to access your written $_SESSION variables? 您是否正在尝试访问写入的$_SESSION变量的其他页面中调用session_start() You will need to do that too before trying to read anything, eg: 在尝试阅读任何内容之前,您还需要这样做,例如:

session_start();
$blah = $_SESSION['blah'];

This is a known problem in PHP, HTTP, or whoever you want to blame. 这是PHP,HTTP或您想要责怪的任何人的已知问题。 Basically, you cannot set cookies and redirect using HTTP in the same request . 基本上,您无法在同一请求中使用HTTP设置cookie和重定向 When you are starting a session that has not been created yet, you are sending a cookie. 当您启动尚未创建的会话时,您正在发送cookie。

Two options: 两种选择:

  1. start the session elsewhere, or 在其他地方开始会议,或
  2. send either JavaScript that forces a redirect, or use a meta tag. 发送强制重定向的JavaScript或使用元标记。

Example: 例:

<?php /* set session cookies */ ?>
<script>window.location.replace("index.php");</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM