javascript 如何在登录 HTML 页面上使用 XML 匹配凭据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19632439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to match credentails using XML on login HTML page?
提问by Rock
Locally I am trying to match the login credentails using the XML page which will act like database. If the login credentails matches, it must proceed to next page which will be new HTML page containing something. Here's what I have till now in HTML & XML page. I did the validation using the JavaScript and hard coded for one user. In XML if I have say 10 users then how do I do?
在本地,我正在尝试使用类似于数据库的 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:   <input type="text" name="userid"/><br><br>
Password:   <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 code:
XML 代码:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<student>
<details>
<username>user1</username>
<password>pass1</password>
<email>[email protected]</email>
</details>
<details>
<username>user2</username>
<password>pass2</password>
<email>[email protected]</email>
</details>
<details>
<username>user3</username>
<password>pass3</password>
<email>[email protected]</email>
</details>
<details>
<username>user4</username>
<password>pass4</password>
<email>[email protected]</email>
</details>
</student>
采纳答案by Bharath R
You can use jQuery and do something like this..
你可以使用 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:   <input type="text" id="userid" name="userid"/><br><br>
Password:   <input type="password" id="pwd" name="paasword"/><br><br>
<input type="button" id="b1" value="Login"/>
<input type="reset" value="Cancel"/>
</div>
</body>
</html>
回答by Joran Den Houting
You could create an XML
file for each user, which will have better performance.
您可以XML
为每个用户创建一个文件,这将具有更好的性能。
Then when login, check if the file exsists and read the password out of it:
然后在登录时,检查文件是否存在并从中读取密码:
<?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";
}
?>
I would suggest to use some password encryption too, like this:
我也建议使用一些密码加密,如下所示:
$password = base_64_encode(md5($_POST['password']));
Ofcourse you'll need to save the passwordt using the same encoding in your XML
files.
当然,您需要在XML
文件中使用相同的编码保存密码。