所以我在存储输入到我的网站的用户名并让自己登录到网站时遇到问题。我可以登录(我看到这是因为“您已登录”消息。但是第二次单击链接转到 members.php 页面时,它返回回显“您尚未登录。”。为什么不是 $username 值被添加到 SESSION 中了吗?
这是我的4个代码。我还有第五个我所有的 MySQL 信息,我不想分享,但是,我的用户名是通过 MySQL 确认的,因为我收到了第一次登录验证。是的,我按照 YouTube 上的教程进行操作。
索引.php
<html>
<form action='login.php' method='POST'>
Username: <input type='username' name='username'><br>
Password: <input type='password' name='password'><br>
<input type='submit' name='button' value='Login'>
</form>
</html>
登录.php
<?php
session_start();
$password = $_POST['password'];
$username = $_POST['username'];
include ("connect.php");
if ($username && $password)
{
// info is provided
$queryget = mysql_query("SELECT * FROM login WHERE username='$username' AND password ='$password' ");
$numrows = mysql_num_rows($queryget);
if ($numrows != 0)
{
$_SESSION['username'] = $username;
echo "You have been logged in. <a href='members.php'>Go to the members page</a>";
}
else echo "This password is wrong";
include ("index.php");
}
else
{
echo "Wrong password, try again!";
include ("index.php");
}
?>
成员.php
<?php
session_start();
$username = $_SESSION['username'];
if ($username)
{
echo "Welcome $username | <a href='logout.php>Logout</a>'";
}
else
{
echo "You are not logged in.";
include ("index.php");
}
?>
注销.php
<?php
session_start();
$username = $_SESSION['username'];
session_destroy();
echo "You have been logged out.";
?>