1

在本地,我正在尝试使用类似于数据库的 XML 页面来匹配登录凭据。如果登录凭据匹配,它必须进入下一页,该页面将是包含某些内容的新 HTML 页面。这是我到目前为止在 HTML 和 XML 页面中所拥有的内容。我使用 JavaScript 进行了验证,并为一位用户进行了硬编码。在 XML 中,如果我有 10 个用户,那我该怎么办?

<html>
<head><title>Login page</title></head>
<body>

<div align="right">
<h1 style="font-family:Times New Roman;text-align="center";font-size:20pt;
color:#00FF00;>
Welcome to Login Page
</h1>
<form name="login">
Username: &nbsp <input type="text" name="userid"/><br><br>
Password:  &nbsp  <input type="password" name="paasword"/><br><br>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)/*function to check userid & password*/
{
/*the following code checkes whether the entered userid and password are matching*/
if(form.userid.value == "user1" && form.password.value == "pass1")
{
window.open('success.html')/*opens the target page while Id & password matches*/
}
else
{
alert("wrong Username or Password")/*displays error message*/
}
}
</script>
</div>
</body>
</html>

XML 代码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<student>

<details>
<username>user1</username>
<password>pass1</password>
<email>user1@abc.com</email>
</details>

<details>
<username>user2</username>
<password>pass2</password>
<email>user2@abc.com</email>
</details>

<details>
<username>user3</username>
<password>pass3</password>
<email>user3@abc.com</email>
</details>

<details>
<username>user4</username>
<password>pass4</password>
<email>user4@abc.com</email>
</details>

</student>
4

2 回答 2

1

你可以使用 jQuery 并做这样的事情..

<html>
<head><title>Login page</title></head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
   $(document).ready(function(){
   var xml;
  $('#b1').click(function(){
       $.get('users.xml', null, function (data, textStatus) {
           xml=data;
            $(xml).find('details').each( function(){
                var item = $(this);

                if(item.find('username').text()==$('#userid').val() && item.find('password').text()==$('#pwd').val())
                {
                    window.open('success.html');
                }
           });
        });
    });
});
    </script>
<body>
<div align="right">
<h1> Welcome to Login Page </h1>
Username: &nbsp <input type="text" id="userid" name="userid"/><br><br>
Password:  &nbsp  <input type="password" id="pwd" name="paasword"/><br><br>
<input type="button" id="b1" value="Login"/>
<input type="reset" value="Cancel"/>
</div>
</body>
</html>
于 2013-10-28T11:21:07.923 回答
1

XML您可以为每个用户创建一个文件,这将具有更好的性能。

然后在登录时,检查文件是否存在并从中读取密码:

<?php
if(isset($_POST['login'])){
$username = preg_replace('/[^A-Za-z]/', '', $_POST['username']);
$password = $_POST['password'];
if(file_exists('users/' . $username . '.xml')){
    $xml = new SimpleXMLElement('users/' . $username . '.xml', 0, true);
    if($password == $xml->password){
        session_start();
        $_SESSION['username'] = $username;
        header('Location: index.php');
        die;
    } else {
                echo "wrong password";
            }
} else {
    echo "User not found";
}
?>

我建议也使用一些密码加密,如下所示:

$password = base_64_encode(md5($_POST['password']));

当然,您需要在XML文件中使用相同的编码保存密码。

于 2013-10-28T10:59:23.960 回答