我最近開始處理表單驗證和登錄/註冊。但是,我遇到了一個問題,當我單擊具有現有用戶名和密碼的登錄按鈕時,我想要轉到同一頁面(登錄頁面)或起始頁面,並顯示消息:「您已成功登錄」。我不知道該怎麼做。我有除其他外,兩個PHP文件,的login.php:登錄表單和註冊PHP
if (loggedin()) {
$firstname = getuserfield('firstname');
$surname = getuserfield('surname');
echo 'You\'re logged in, '.$firstname.' '.$surname.'. <a href="logout.php">Log out </a><br>';
} else {
include 'loginform.inc.php';
}
和loginform.inc.php
if (isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash = md5($password);
if (!empty($username)&&!empty($password)) {
$query = "SELECT `id` FROM `users` WHERE `username`='".mysql_real_escape_string($username)."' AND `password`='".mysql_real_escape_string($password_hash)."'";
if ($query_run = mysql_query($query)) {
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0) {
?> <div id = "a5"> <?php echo 'No such combination of username and password' ?> </div>; <?php
} else if ($query_num_rows==1){
$user_id = mysql_result($query_run, 0, 'id');
$_SESSION['user_id']=$user_id;
header('Location: login.php');
}
}
} else {
?> <div id="a"> <?php echo 'Please enter username and password!' ?> </div>
爲什麼在驗證用戶憑據後,您不重定向到成功頁面?並在成功頁面上,只需檢查會話是否設置! – NoobEditor