$ _SESSION值不成立! [英] $_SESSION values not holding!
问题描述
我正在编写一个用户登录系统,我(和许多其他人一样)在会话方面遇到问题.
I'm writing a user login system, and I (like so many others) am having a problem with my sessions.
这是验证输入后登录脚本的指针:
Here's the pointer from the login script when the inputs are validated:
session_start();
$_SESSION['id']=$id;
header('location: memberhome.php');
这是memberhome.php上的第一件事:
Here's the first thing on memberhome.php:
<?php
session_start();
$id=$_SESSION['id'];
?>
稍后在memberhome.php中:
And later in memberhome.php:
You are logged in as: <?php echo $id; ?>
问题是$ _SESSION ['id']显然是空的,因此回显$ id没有显示任何内容.
The problem is $_SESSION['id'] is apparently empty so the echo $id prints nothing.
另一种无效的方法:
//removed session_start and $_SESSION bit from the top
You are logged in as: <?php session_start(); echo $_SESSION['id']; ?>
现在,这是奇怪的部分.此方法有效:
NOW, here's the weird part. This method DOES work:
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
您可以看到session_start()在回显之后被移动了.当页面从登录脚本加载时有效.但是,刷新后,它将不再起作用.
You can see the session_start() is moved AFTER the echo. This works when the page loads from the login script. However, upon refresh, it does NOT work once again.
我尝试了很多替代方法,并花了几个小时来寻找先前问题的答案.我还查看了phpinfo()的内容,但没有发现任何问题.这完全是我的进步.谢谢!
I've tried a bunch of alternatives and spent a few hours searching for answers in previous questions. I also looked at my phpinfo() for something fishy and found nothing. This is entirely what my progress is hinging on. Thanks!
推荐答案
首先,请启用调试:
error_reporting(E_ALL);
ini_set('display_errors', '1');
第二,session_start()必须位于页面顶部.所以你写的那行;
Second, session_start() needs to be at the top of the page. So the line you wrote;
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
将永远无法工作.
在任何HTML等之前,以下行必须位于页面顶部.
The following line needs to be on top of the page, before any HTML etc.
<?php
session_start();
$id=$_SESSION['id'];
?>
这篇关于$ _SESSION值不成立!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!