[英]$_SESSION not carrying across php pages
I am having a problem with the $_SESSION
function in my php website. 我的php网站中的$_SESSION
函数有问题。 I set the function and then try to access it in another page, but it just comes up as blank. 我设置了功能,然后尝试在另一个页面中访问它,但是它只是空白。
This is the file login.php
where I set the $_SESSION
这是我设置$_SESSION
的文件login.php
<?php
session_start();
?>
<html>
<?php
$email = $_POST['email'];
$password = $_POST['password'];
$con = mysqli_connect("host","user","pass","db");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
if ($email && $password){
$result = mysqli_query($con, "SELECT * FROM users WHERE email='$email'");
$numrows = mysqli_num_rows($result);
if ($numrows != 0){
//username exists
$row = mysqli_fetch_row($result);
if ($password != $row[2]){
print 'Incorrect password<br />';
$error = TRUE;
}
else {
//password is correct
echo "You're in! <a href='member.php'>Click</a> here to enter member page";
$_SESSION['email'] = $email;
}
}
else {
//email not found
print 'Sorry, that email cannot be found.<br />';
$error = TRUE;
}
}
else {
die('Please enter an email and password<br />');
$error = TRUE;
}
if ($error){
print '<a href=index.php>Go Back</a>';
}
?>
</html>
And this is the member.php
file. 这是member.php
文件。
<?php
session_start();
if (isset($_SESSION['email'])){
$username = $_SESSION['email'];
echo "Your email is ";
echo $username;
}
?>
When I press the link to go the the member.php
page via the link, the page is completely blank, presumably suggesting that the $_SESSION['email']
has nothing in it. 当我按下链接以通过链接转到member.php
页面时,该页面完全空白,可能表明$_SESSION['email']
中没有任何内容。
Please let me know where I am going wrong, and how to rectify the issue. 请让我知道我要去哪里错了,以及如何解决此问题。
Any help would be much appreciated. 任何帮助将非常感激。
If anyone wants to see how the site is actually working, please go here . 如果有人想查看该站点的实际运行情况,请转到此处 。
您需要在login.php的最顶部而不是中途进行此调用。
session_start();
Add session_start(); 添加session_start(); before the HTML in login.php to set it 在login.php中的HTML之前进行设置
Add this to the very top of the login.php page: 将其添加到login.php页面的最顶部:
<?php
session_start ();
?>
From the PHP documentation: 从PHP文档中:
"Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser." “注意:要使用基于cookie的会话,必须在输出任何内容到浏览器之前调用session_start()。”
http://us3.php.net/session_start http://us3.php.net/session_start
If that still doesn't work edit this line: 如果仍然无法编辑此行:
echo "You're in! <a href='member.php'>Click</a> here to enter member page";
to be like this instead: 改为这样:
echo "You're in! <a href='member.php?" . htmlspecialchars(SID) . "'>Click</a> here to enter member page";
http://us3.php.net/manual/en/session.idpassing.php http://us3.php.net/manual/zh/session.idpassing.php
Z ž
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.