The Objective
To fetch the data from the XML "database" as a simple (and I know, very insecure) way of verifying login data.
The code here is that responsible for the database and for retrieving the values. Option one is what I originally tried, so it still contains the code which cross-references the input data of the username and password with the data in the XML database. Option two is where I tried a different method. Neither seem to be working. Screenshots attached.
Option One:
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>
Option Two:
XML code "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 code:
<?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'];
?>
Result:
As you can see, it grabs the XML data and the array and how it is structured cen be viewed in the screenshot. I am able to get the username, but if I try to specify the password (echo $nodes[0]['password'];) as the thing I want, it comes up with the second image below: