我正在嘗試讓用戶登錄。如果密碼和用戶名錯誤,我想要一個彈出窗口來提醒用戶出現錯誤。當他們關閉警報時,它會返回到index.php,這將返回到登錄屏幕。在php文件中彈出提醒腳本
但是,當它是錯誤的密碼/用戶名,最終返回到index.php首先沒有任何彈出消息。我的瀏覽器設置不阻止任何彈出窗口。我可以知道我做錯了嗎?
<?php
if($login == true){
//Do login process
//this portion works as long as correct username and password
}
else{
echo '<script language="javascript">alert("Please enter valid username and password");</script>';
header("location:index.php");
}
?>
//login.php
<?php
$username = "exampleuser";
$password = "examplepass";
$host = "localhost";
$dbHandle = mysql_connect($host, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("database_name", $dbHandle);
$myUserName = $_POST['user'];
$myPassword = $_POST['pass'];
if(ctype_alnum($myUserName) && ctype_alnum($myPassword)){
$query1 = "SELECT * FROM users WHERE username='$myUserName'";
$result1 = mysql_query($query1);
$count1 = mysql_num_rows($result1);
if($count1 == 1){
$query2 = "SELECT password FROM users WHERE username='$myUserName'";
$result2 = mysql_query($query2);
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
$pass = $row['password'];
if(password_verify($myPassword, $pass)){
$seconds = 120 + time();
setcookie(loggedIn, date("F js - g:i a"), $seconds);
header("location:mysite.php");
}
else{
echo '<script language="javascript">
alert("Please enter valid username and password");
window.location.href = "http://index.php";
</script>';
die();
}
}
else{
echo '<script language="javascript">
alert("Please enter valid username and password");
window.location.href = "http://index.php";
</script>';
die();
}
}
else{
echo '<script language="javascript">
alert("Please enter valid username and password");
window.location.href = "http://index.php";
</script>';
die();
}
?>
執行'echo'後PHP不停止。它繼續告訴瀏覽器轉向'index.php',因爲所有這些都發生在服務器上,而不是在用戶的瀏覽器中。 –
@TillHelge如果我完全刪除頭(),也不起作用。我最終看到一個空白頁面,而不是回到index.php –