簡體   English   中英

php:如何重定向到帶有錯誤消息的同一頁面

[英]php: How can I redirect to the same page with a error message

當輸入 email 不包含 @ 符號時,我需要重定向到同一頁面。 代碼的 rest 正在工作,但是每當我放置一個沒有@的 email 時,它都會將我重定向到一個空白頁面。 (問題詳情如下:登錄屏幕需要對其輸入數據進行一些錯誤檢查。如果名稱或密碼字段為空,您應該顯示以下表單的消息:

Email 和密碼是必需的

請注意,我們使用“電子郵件”而不是“用戶名”來登錄此作業。

如果密碼不為空且不正確,您應該提出以下形式的消息:

密碼錯誤

對於此分配,您必須添加一個新驗證以確保登錄名包含 at 符號 (@) 並在這種情況下發出錯誤:

Email 必須有一個 at 符號 (@)

如果傳入的密碼經過正確哈希處理,與存儲的 stored_hash 值匹配,則用戶的瀏覽器將重定向到 autos.php 頁面,其中用戶名作為 GET 參數使用:

header("位置:autos.php?name=".urlencode($_POST['who']));

您還必須使用 error_log() function 在用戶由於密碼錯誤導致登錄失敗時發出以下消息,顯示密碼的計算 hash 加上鹽:

error_log("登錄失敗 ".$_POST['who']." $check");

當登錄成功時(即 hash 匹配)發出以下日志消息:

error_log("登錄成功".$_POST['who']); )

<?php 

if ( isset($_POST['cancel'] ) ) {
    header("Location: index.php");
    return;
}

$salt = 'XyZzy12*_';
$stored_hash = '1a52e17fa899cf40fb04cfc42e6352f1';  

$failure = false;  


// Check to see if we have some POST data, if we do process it
if ( isset($_POST['email']) && isset($_POST['pass']) ) {
    if ( strlen($_POST['email']) < 1 || strlen($_POST['pass']) < 1 ) {
        $failure = "Email and password are required";
    } else {
        if(strpos($_POST['email'], '@') !== false)
        {
            $check = hash('md5', $salt.$_POST['pass']);
            if ( $check == $stored_hash ) {
                // Redirect the browser to autos.php
                header("Location: autos.php?name=".urlencode($_POST['email']));
                error_log("Login success ".$_POST['email']);
                return;
            } else {
                $failure = "Incorrect password";
                error_log("Login fail ".$_POST['email']." $check");
            }             
        }
        else
        {
            $failure = "Email must have an at-sign @";
            return;
        }

    }
}

// Fall through into the View
?>
<!DOCTYPE html>
<html>
<head>
<?php require_once "bootstrap.php"; ?>
<title>ANIK CHAKRABORTI</title>
</head>
<body>
<div class="container">
<h1>Please Log In</h1>
<?php
// Note triple not equals and think how badly double
// not equals would work here...
if ( $failure !== false ) {
    // Look closely at the use of single and double quotes
    echo('<p style="color: red;">'.htmlentities($failure)."</p>\n");
}
?>
<form method="POST">
<label for="nam">Email</label>
<input type="text" name="email" id="nam"><br/>
<label for="id_1723">Password</label>
<input type="text" name="pass" id="id_1723"><br/>
<input type="submit" value="Log In">
<input type="submit" name="cancel" value="Cancel">
</form>

</div>
</body>

$failure = "Email must have an at-sign @"行之后從 else 塊中刪除return

我認為,在不知道如何處理請求的情況下,根據驗證失敗的示例,該過程不得返回。 它只會在成功時返回,因為在這種情況下,您將擁有一個新的 header ,因此當頁面加載時它會將您帶到某個地方,失敗時您只需更改$failure變量即可。

刪除返回的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM