简体   繁体   English

php:如何重定向到带有错误消息的同一页面

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

I need to redirect to the same page when the input email does not contain an @ sign.当输入 email 不包含 @ 符号时,我需要重定向到同一页面。 The rest of the code is working but whenever I put an email without an @, it's redirecting me to a blank page.代码的 rest 正在工作,但是每当我放置一个没有@的 email 时,它都会将我重定向到一个空白页面。 (The details of the question are as follows: The login screen needs to have some error checking on its input data. If either the name or the password field is blank, you should display a message of the form: (问题详情如下:登录屏幕需要对其输入数据进行一些错误检查。如果名称或密码字段为空,您应该显示以下表单的消息:

Email and password are required Email 和密码是必需的

Note that we are using "email" and not "user name" to log in in this assignment.请注意,我们使用“电子邮件”而不是“用户名”来登录此作业。

If the password is non-blank and incorrect, you should put up a message of the form:如果密码不为空且不正确,您应该提出以下形式的消息:

Incorrect password密码错误

For this assignment, you must add one new validation to make sure that the login name contains an at-sign (@) and issue an error in that case:对于此分配,您必须添加一个新验证以确保登录名包含 at 符号 (@) 并在这种情况下发出错误:

Email must have an at-sign (@) Email 必须有一个 at 符号 (@)

If the incoming password, properly hashed matches the stored stored_hash value, the user's browser is redirected to the autos.php page with the user's name as a GET parameter using:如果传入的密码经过正确哈希处理,与存储的 stored_hash 值匹配,则用户的浏览器将重定向到 autos.php 页面,其中用户名作为 GET 参数使用:

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

You must also use the error_log() function to issue the following message when the user fails login due to a bad password showing the computed hash of the password plus the salt:您还必须使用 error_log() function 在用户由于密码错误导致登录失败时发出以下消息,显示密码的计算 hash 加上盐:

error_log("Login fail ".$_POST['who']." $check"); error_log("登录失败 ".$_POST['who']." $check");

When the login succeeds (ie the hash matches) issue the following log message:当登录成功时(即 hash 匹配)发出以下日志消息:

error_log("Login success ".$_POST['who']); 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>

Remove the return from the else chunk after the $failure = "Email must have an at-sign @" line.$failure = "Email must have an at-sign @"行之后从 else 块中删除return

I think, without knowing much of how the request is being handled, that according to the example when validation fails the process must not return.我认为,在不知道如何处理请求的情况下,根据验证失败的示例,该过程不得返回。 It only returns on success because in that case you would have a new header and so when the page loads it gets you somewhere, on failure you need to only change the $failure variable and that is it.它只会在成功时返回,因为在这种情况下,您将拥有一个新的 header ,因此当页面加载时它会将您带到某个地方,失败时您只需更改$failure变量即可。

删除返回的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM