我设置了 login.php
,以便每当登录成功时,它都会自动定向到 profile.php
。但是,即使登录成功,每当我访问profile.php
时,它仍然显示“您必须登录才能访问此页面”。为什么?
这是我的login.php
:
$email = $_POST['email-field'];
$password = $_POST['password-field'];
if ($email && $password)
{
$connect = mysql_connect("xx", "xx", "xx") or die("Couldnt connect!");
mysql_select_db(xx) or die("Couldnt find db");
$query = mysql_query("SELECT * FROM users WHERE email = '$email'");
$numrows = mysql_num_rows($query);
if ($numrows != 0)
{
while ($row = mysql_fetch_assoc($query))
{
$email = $row['email'];
$md5password = $row['password'];
}
if ($email == $email && $md5password == md5($password))
{
header("Location: profile.php");
$_SESSION['email']==$email;
}
else
echo "Incorrect password";
}
else
die("That user doessnt exist!");
}
else
die("Please enter a username and a password");
还有我的profile.php
:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
include('get-info.php');
session_start();
if ($_SESSION['email'])
echo "<a href=logout.php>Logout</a>";
else
die("You must be logged in to access this page");
?>
<html>
<head>
<title>
R-A | Profile
</title>
<link href='css.css' rel='stylesheet' type='text/css' />
</head>
<body class='body3' >
<div class='logo-bar'>
<div class='menu'>
<img src='images/rateaway.png' class='logo-bar-img' />
<div class='menu-options'>
<a href='index.php' class='menu-links' >Home</a>
<a href='profile.php' class='menu-links' >Profile</a>
<a href='profile.php' class='menu-links' >Friends</a>
</div>
</div>
</div>
<div class='profile-main-content' >
<div class='profile-current-info'>
<p class='profile-name'> <?php get_info('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e071c0f0031030f1a0b1b2e170f060101400d0103" rel="noreferrer noopener nofollow">[email protected]</a>', 'name'); ?> </p>
<img src='<?php get_info('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb92899a95a4969a8f9e8ebb829a939494d5989496" rel="noreferrer noopener nofollow">[email protected]</a>', 'profilepic'); ?>' class='profile-pic'/>
<p class='profile-dob' > Born: <?php get_info('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adc4dfccc3f2c0ccd9c8d8edd4ccc5c2c283cec2c0" rel="noreferrer noopener nofollow">[email protected]</a>', 'dob'); ?> </p>
<p class='profile-country' > Currently lives in: <?php get_info('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d8c3d0dfeedcd0c5d4c4f1c8d0d9dede9fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a>', 'country'); ?> </p>
<p class='profile-gender' > Gender: <?php get_info('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b1aab9b687b5b9acbdad98a1b9b0b7b7f6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>', 'gender'); ?> </p>
</div>
<div class='profile-edit-info' >
</div>
</div>
</body>
最佳答案
您忘记开始 session 。在每个想要使用 session 的页面上,您必须调用:
session_start();
还要确保在输出任何内容之前执行此操作,否则它将无法工作。
关于php - "You must be logged in to access this page"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924486/