目標
從XML「數據庫」中以簡單(並且我知道非常不安全)的方式驗證登錄數據。XML Database Array做奇怪的事情。嘗試使用XML和PHP驗證登錄
這裏的代碼是負責數據庫和檢索值的。選項一是我最初嘗試的,所以它仍然包含交叉引用用戶名和密碼的輸入數據與XML數據庫中的數據的代碼。 選項二是我嘗試不同方法的地方。似乎都沒有工作。 附上截圖。
方案一:
的login.php:
<?php
if(!isset($_SESSION['user'])) { ?>
<div>Please login to view your message log, and see whether anyone has left you a message.</div>
<form action="php/authenticate.php" method="POST" >
<input type="text" name="Username" />
<input type="password" name="Password" />
<input type="submit" value="Login" />
</form>
<?php };
$passwordValid = $_GET['error'];
if($passwordValid == 1) {
echo "Falsches Password";
};
?>
authenticate.php:
<?php
session_start();
// Load Credential Database
$xml = new DOMDocument();
$xml->load('../logins.xml');
$xpath = new DOMXPath($xml);
// Variable Declarations
$user = $_POST['Username'];
$password = $_POST['Password'];
// XPath Query for Correct Credential
$queryUsers = '//authentication/client/user[. = "' . $user . '"]';
$userActual1 = $xpath->query($queryUsers);
$userActual = $userActual1->nodeValue; // Output: Anton
$passwordActual1 = $userActual1 . 'following-sibling::*[1]';
$passwordActual = $xpath->query($passwordActual1); // Output: damn
// Authentication Checker
if($user == 'Arend' && $password == 'damn') {
$userLogin = true;
$_SESSION['user'] = $user;
header('Location: ../index.php');
} else {
$userLogin = false;
header('Location: ../index.php?error=1');
};
if($userLogin == true) {
header('Location: ../index.php');
};
?>
XML 「logins.xml」:
<?xml version="1.0" encoding="UTF-8"?>
<authentication>
<client>
<user>Arend</user>
<password>damn</password>
</client>
<client>
<user>Felipe</user>
<password>damned</password>
</client>
<client>
<user>Paula</user>
<password>damnest</password>
</client>
</authentication>
方案二:
XML代碼 「的test.xml」:
<?xml version="1.0" encoding="UTF-8"?>
<authentication>
<user name="Arend">
</user>
<password>damn</password>
<user name="Paula">
</user>
<password>damnest</password>
</authentication>
PHP代碼:
<?php
$xml = simplexml_load_file('test.xml');
$nodes = $xml->xpath('//authentication/user[@name="Arend"]');
// Variable Declarations
$user = "Arend";
$password = "damn";
echo 'second test' . '<br>';
print_r ($nodes);
echo '<br>';
print_r ($nodes[0]['name']);
echo '<br>';
echo $nodes[0]['name'];
?>
結果:
正如你所看到的,它抓住了XML數據和數組以及它在結構中的結構如何在屏幕截圖中查看。我能夠獲得用戶名,但是如果我嘗試指定密碼(echo $nodes[0]['password'];
)作爲我想要的內容,則會出現以下第二張圖像:
我只是要藉此機會指出,這實際上是在不安全實際上完全不顧您作爲系統設計者的責任。你永遠不應該保存純文本密碼。看看bcrypt,特別是如果你打算像這樣保存它們。 – MiDri
同意MiDri。考慮到他的觀點,我不認爲你的XML是最好的格式。密碼節點應該是用戶的孩子,或者可能是屬性,例如, ' asdf '或' ' –
rjdown
選項2肯定不明智(忽略整個logins.xml是壞的想法),因爲他們沒有共享的父母。密碼和用戶應該以某種方式分組。所以我想說選擇1是這樣做的唯一有價值的選擇。 – JPMC