使用XML进行PHP登录验证

时间:2018-03-12 01:28:39

标签: php html xml login xml-parsing

每次登录都会进入shop.php页面。我很困惑,我做错了。有人在这里为我解决这个问题吗?感谢

<?php
    $xml = simplexml_load_file("info.xml");
 if (isset($_POST['submit'])) {
     $user=$_POST["username"];
     $pass=$_POST["password"];
     $account=$_POST["accounttype"];

     foreach ($xml->author as $author) {
         if ($user==$author->aName && $pass==$author->apassword && 
 $account==$author->$author->{"Buyer"}) {
             session_start();
             $_SESSION['simple_login'] = $user;
             $_SESSION['pass_login'] = $pass;
             header("location:shop.php");
             exit();
         } elseif ($user==$author->aName && $pass==$author->apassword && 
  $account==$author->$author->{"Seller"}) {
             session_start();
             $_SESSION['simple_login'] = $user;
             $_SESSION['pass_login'] = $pass;
             header("location:admin.php");
             exit();
         } else {
             $error = '<div class="alert alert-danger" role="alert"><p>
  <strong>Invalid credentials. Try again!</div>';
         }
     }
 }
?>

我的XML是

<?xml version="1.0"?>
<authorList>
    <author>
        <aName>bbb</aName>
        <apassword>bbb</apassword>
        <aemail>email@yahoo.com</aemail>
        <aphone>111</aphone>
        <aaccounttype>Buyer</aaccounttype>
    </author>
    <author>
        <aName>sss</aName>
        <apassword>sss</apassword>
        <aemail>test@yahoo.com</aemail>
        <aphone>222</aphone>
        <aaccounttype>Seller</aaccounttype>
    </author>
</authorList>

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需更改两个if条件:

if (... $account == $author->$author->{"Buyer"})
if (... $account == $author->$author->{"Seller"})

应该成为:

if (... $account == $author->aaccounttype)
if (... $account == $author->aaccounttype)

您的$_POST变量accounttype应分别映射到BuyerSeller

答案 1 :(得分:0)

您似乎在检查传入的帐户类型是否与记录中的类型匹配,您只需要检查XML文件中的值是买方还是卖方......

if ($user==$author->aName && $pass==$author->apassword &&
        $author->aaccounttype == "Seller" ) {

if ($user==$author->aName && $pass==$author->apassword &&
        $author->aaccounttype == "Buyer" ) {