使用 时header("Location: url.php)
,在我们的例子中,URL 不会更改为新页面login.php
(因此刷新时它只会返回登录页面)。如果页面以表单形式运行,则无需登录。所以我现在的问题是,是否有人知道我们如何使用正确的 URL 访问正确的页面。或者也许有人知道可能导致这种情况的原因。
Login.php(逻辑)
$user = new User();
if(!empty($_POST["btnLogin"])) {
try {
$user = new User();
$user->Username = $_POST["username"];
$user->Password = $_POST["password"];
$user->Login();
}
catch(Exception $e) {
$feedback_error = $e->getMessage();
}
}
login.php(表单)
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input id="username" type="text" name="username" placeholder="username" />
<input id="password" type="password" name="password" placeholder="password" />
<p>Not yet signed up? <a href="register.php" >Register</a></p>
<input type="submit" name="btnLogin" data-theme="b" value="Sign in">
</form>
user.php(类)
public function Login() {
$salt = "ab4p73wo5n3ig247xb1w9r";
$db = new Db();
$select = "SELECT * FROM users WHERE username = '" .
$db->mysqli->real_escape_string($this->Username) .
"' AND password = '" .
$db->mysqli->real_escape_string(md5($this-> Password . $salt)) . "';";
$result = $db->mysqli->query($select);
if($result->num_rows == 1) {
$_SESSION["loggedin"] = true;
$_SESSION["username"] = $this->Username;
//header("Location: show_bugs.php");
} else {
throw new Exception("Please enter correct username and password");
}
}