简体   繁体   English

php 重定向到显示错误结果的另一个页面

[英]php redirecting to another page showing wrong results

I've been trying to verify login info with data stored in an xml file using php.我一直在尝试使用 php 存储在 xml 文件中的数据来验证登录信息。 If the email id and password don't match, it's supposed to echo an error message saying "sorry they don't match".如果 email id 和密码不匹配,它应该回显一条错误消息,说“对不起,它们不匹配”。 If they match, the user is supposed to be redirected to index.html.如果它们匹配,用户应该被重定向到 index.html。 My code keeps showing the error message even if I try to log in with the correct email and password.即使我尝试使用正确的 email 和密码登录,我的代码仍然显示错误消息。 My html form:我的 html 表格:

<form method="post" action="login.php">
<input id='email1' type="email" name="email" placeholder="Email Adress" required="required" class="input-txt" onchange="checkuser();"  /> &nbsp;
<input id='pass1' type="password" name="password" placeholder="Password" required="required" class="input-txt" />
<button type="submit" class="btn" name="ok" id="b1" >Sign in </button> <br> <br>

my xml file(storedata.xml):我的 xml 文件(storedata.xml):

<document>
<data><fname>Group</fname><lname>Project</lname><email2>webuy55@gmail.com</email2><pass2>teamproject55</pass2></data>
<data><fname>Sam</fname><lname>Kim</lname><email2>samkim@gmail.com</email2><pass2>abcd1234</pass2></data>
<data><fname>Harold</fname><lname>Pain</lname><email2>hpain@gmail.com</email2><pass2>abcd</pass2></data>
<data><fname>Ryan</fname><lname>Reynold</lname><email2>ryan@gmail.com</email2><pass2>123456</pass2></data>
<data><fname>Reece</fname><lname>Simpson</lname><email2>reece@gmail.com</email2><pass2>onlinestore</pass2></data>
<data><fname>Stefani</fname><lname>Germanotta</lname><email2>stef@gmail.com</email2><pass2>bbbcccddd</pass2></data>
</document>

my php code(login.php):我的 php 代码(login.php):

<?php 
$emailid= $_POST["email"];
$pass= $_POST["password"];
if(isset($_REQUEST['ok'])) {
$xml2 = simplexml_load_file('storedata.xml');
foreach($xml2->data as $user) { // for every user node
    if($user->email2 == $emailid && $user->pass2 == $pass) {
       header("Location: index.html", true, 301);
       exit();
    }
    else {
        echo 'Sorry, email and passwords dont match.<br>
        <p>Please go back to the login page and enter correct information.</p>';
        break;
    }
}
if (($_POST["email"]== "webuy55@gmail.com") && ($_POST["password"]== "teamproject55"))
{
header("Location: backstore_inventory_ben.html", true, 301);
exit();
}
}
?>

In your foreach loop, you are only checking the first entry in the XML data, then bailing out if it is not a match.在您的 foreach 循环中,您只检查 XML 数据中的第一个条目,如果不匹配则退出。 You need to do something like this:你需要做这样的事情:

<?php
$emailid = $_POST["email"];
$pass    = $_POST["password"];
if (isset($_REQUEST['ok']))
{
    $xml2 = simplexml_load_file('storedata.xml');
    $validLogin = false;
    foreach ($xml2->data as $user)
    {
        // for every user node
        if ($user->email2 == $emailid && $user->pass2 == $pass)
        {
            $validLogin = true;
            break;
        }
    }

    if ($validLogin)
    {
        header("Location: index.html", true, 301);
        exit();
    }
    else
    {
        echo 'Sorry, email and passwords dont match.<br><p>Please go back to the login page and enter correct information.</p>';
    }
    
    if (($_POST["email"] == "webuy55@gmail.com") && ($_POST["password"] == "teamproject55"))
    {
        header("Location: backstore_inventory_ben.html", true, 301);
        exit();
    }
}

Also, if you are going to have more than a few entries in your XML file, you may want to check out DOM and specifically DOMXpath so you can search for a match without having to iterate through the whole ist and do string matching.此外,如果您的 XML 文件中有多个条目,您可能需要检查DOM ,特别是DOMXpath ,这样您就可以搜索匹配项,而无需遍历整个 ist 并进行字符串匹配。

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

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