嘿那里我试图登录并注册系统,当用户登录标题时,它会向他显示一条消息,用他的用户名问候他,当他没有登录时,它会向他显示一条消息,称他为客人。 我的问题是我想在页眉页面(member.php页面)中没有保存会话,用户从未登录。 在测试我的代码时。用户登录成功后,将其重定向到member.php页面,在那里它应该显示用户名和所有内容。但事实并非如此。 这是我的代码:
member.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
session_start();
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<LINK REL=StyleSheet HREF="css/styletest.css" TYPE="text/css">
</head>
<body>
<div id="wrapper">
<!-- Website menu -->
<div id="menu-bar">
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
</div>
<!-- End of Website menu -->
<div id="content">
<!-- Website header -->
<div id="header">
<h1>Testing header</h1>
<?php
if ($username && $userid) {
echo "Welcome <b>$username</b>, <a href='./logout.php'>Logout</a>";
} else {
echo "Please login to access this page. <a href='./login.php'>Login here</a>";
}
?>
</div>
<!-- End of Website header -->
<!-- -----Website content----- -->
<p>Testing website content</p>
<!-- End of Website content -->
</div>
<!-- Website footer -->
<div id="footer">
</div>
<!-- End of Website footer -->
</div>
</body>
</html>
这是login.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
session_start();
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<LINK REL=StyleSheet HREF="css/styletest.css" TYPE="text/css">
</head>
<body>
<div id="wrapper">
<!-- Website menu -->
<div id="menu-bar">
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
<li><a href="">Testing links</a></li>
</div>
<!-- End of Website menu -->
<div id="content">
<!-- Website header -->
<div id="header">
<h1>Testing header</h1>
<?php
if ($username && $userid) {
echo $username;
} else {
echo 'Welcome guest';
}
?>
</div>
<!-- End of Website header -->
<!-- -----Website content----- -->
<h2>Log in</h2>
<?php
$form='<form action="login.php" method="POST">
<table>
<tr>
<td>Username :</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="loginbtn" value="Log in"></td>
</tr>
</table>
</form>';
$user = $_POST['user'];
$pass = $_POST['pass'];
if ($_POST['loginbtn']) {
if ($user) {
if ($pass) {
require 'core/connect.php';
$query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
$row = mysql_fetch_assoc($query);
$passwordFromPost = $_POST['pass'];
$hashedPasswordFromDB = $row['password'];
if (password_verify($passwordFromPost, $hashedPasswordFromDB)) {
$query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
$numrows = mysql_num_rows($query);
if ($numrows == 1) {
$query = mysql_query("SELECT * FROM users WHERE username = '$user' ");
$row = mysql_fetch_assoc($query);
$dbactive = $row['active'];
$dbuser = $row['username'];
if ($dbactive == 1) {
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
echo "You have been logged in as <b>$dbuser</b> <a href='./member.php'>Click here</a> to go back to home page";
} else {
echo '<font color="red">You must activate your account to log in.</font>';
echo $form;
}
} else {
echo '<font color="red">You entered an invalid username or password.</font>';
echo $form;
}
} else {
echo '<font color="red">You entered an invalid username or password.</font>';
echo $form;
}
mysql_close();
} else {
echo '<font color="red">You must enter your password.</font>';
echo $form;
}
} else {
echo '<font color="red">You must enter your username.</font>';
echo $form;
}
}else{
echo $form;
}
?>
<!-- End of Website header -->
<!-- -----Website content----- -->
<p>Testing website content</p>
<!-- End of Website content -->
</div>
<!-- Website footer -->
<div id="footer">
</div>
<!-- End of Website footer -->
</div>
</body>
</html>
现在我不认为在login.php中有任何问题,因为我测试了页面,即使这段代码也能正常工作:
echo "You have been logged in as <b>$dbuser</b> <a href='./member.php'>Click here</a> to go back to home page";
如果它在member.php中显示了他的用户名,则没有任何反应。 谢谢你的阅读。
答案 0 :(得分:0)
对我而言,似乎变量$username
和$userid
未在任何地方初始化。由于您的问题涉及会话,我假设您从会话中获取这些变量。如果他们在其他地方被宣布,则会话未被使用(根据您的代码)。
在您编写的member.php
文件中:
$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;
这似乎是错误的,因为您重新声明会话中的值而不是简单地获取它们。请改用以下内容。
$username = isset($_SESSION['username']) ? $_SESSION['username'] : false;
$userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
这将检查用户名和用户ID是否存储在会话中。如果它们丢失,则为变量分配值false
。然后,当您进一步检查变量时,条件将失败并执行关于访客用户的块。
<强>加成强>
在使用变量之前,您还应该习惯检查变量是否存在。这有助于您避免undefined variable
错误。使用isset
关键字进行检查。这可能看起来更有效,但代码的健壮性增益无法与您(可能)花费在修复简单错误上的时间进行比较。
希望这有帮助,快乐编码!