1

I am not that great with XML but I'm pretty good with PHP. I need to use an XML file as a "database" of sorts. It contains various user information. Including login information. The XML is set up as such:

<employee ssnum="">
<first_name></first_name>
<last_name></last_name>
<contact_info>
    <office_phone></office_phone>
    <email></email>
    <cell_phone></cell_phone>
</contact_info>
<access_info level="user">
    <username></username>
    <password></password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>

I need some way to check the username and password to see if they match, and log in the respective user. I am unsure of how to check the username/password pairs, and how to return the proper user for login. There was an alternate way to do this I was shown via DOMdocument and then mysql queries to use the xml file as a database of sorts, but I'm not sure that is the easiest way, or how that would work.

This is not for any serious kind of work, it is a learning project.

Any helps would be appreciated, thanks.

sharf
  • 2,123
  • 4
  • 24
  • 47

3 Answers3

2

Seems that you was told bollocks about DOMDocument. Or - more likely - you misunderstood something. You'll not use mysql queries, you'll use xpath queries to access nodes in the xml. You should indeed use DOMDocument and DOMXPath to access the nodes.

Here comes an example how to read or modify the password:

$doc = new DOMDocument();
$doc->load('employee.xml');

$selector = new DOMXpath($doc);

$result = $selector->query('/employee/access_info/password');

// read password:
$password = $result->item(0)->nodeValue;

// set password:
$result->item(0)->nodeValue = 'secret';

// save xml
$doc->save('employee.xml');

If you need more info about XPath you can follow this tutorial.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Not sure if I just did something wrong, but when I echo $password, it echoes the username, not the password. – sharf Apr 21 '13 at 22:37
  • You didn't do anything wrong, just change `/employee/access_info/username` to `/employee/access_info/password` ^^ – Jon Apr 21 '13 at 22:39
  • I'm beginning to get this. But it only grabs the first password (assuming because of item(0)) So I would have to loop through each user, comparing the username/password to see if both match. I would think a for loop would suffice, but then how do I tell how many employees there are (for the $i – sharf Apr 21 '13 at 22:46
  • Follow the tutorial which I have linked. It will explain anything you need. (in ~30 minutes) – hek2mgl Apr 21 '13 at 22:48
  • I've read through that and it mostly answered my questions. I still have one, plus an issue I can't resolve. First, I did not see in there how to count the number of elements I am grabbing, I understand it is in an array, but I'm still not sure how to count that (perhaps count()?). The next thing, is that using a for loop with a set number for testing, I have it loop through 0-1, and checks if the username and password given are equal to the username and password for item(0), and item(1). The issue I have is that as long as the username is right, it will "log in". – sharf Apr 21 '13 at 23:10
  • Seems that you are looking for `DOMNodeList::length`. Refer to the manual of `DOMXPath` (I've linked it). Btw, (I mean this good, not bad) --> learn how to use the php manual on http://www.php.net – hek2mgl Apr 21 '13 at 23:17
  • 1
    I've just about got that part done. It should be enough for me to do what I need. Thanks a ton. Btw, I do try to use the php manual and Google as much as I can. But I have been having trouble with this XML related stuff and really needed some personal help. Thanks again. – sharf Apr 21 '13 at 23:30
0

My first thought was to use xml_parse or, as suggested by Jon, use SimpleXML example in PHP manual also, see this question

Community
  • 1
  • 1
BillK
  • 317
  • 2
  • 11
0

Just a few things I hopefully can help you straighten out a bit, and just expand upon hek2mgl's good and proper advice. I realize it is learning and such but you will not need a loop at all when this is done correctly (assuming of course that you only need to access the one current employee attempting to login). In the tutorial which hek2mgl linked to, it shows you what Predicates are all about, and you should use one to auto-select the correct employee right away in the xpath query, via the username the user entered, then compare the password with the password entered by the user and see if they match. This way there is no need for knowing anything about the $result length (except of course that it has a valid length of 1). Assuming your xml file which contains all the employee elements has a root container element named 'employees' as it likely should:

<?php

//below is php heredoc string.
//just a simulation of an employees.xml file which
//obviously contains multiple employee elements
//within a root 'employees' element
$xmlStr = <<<XMLBookendMarker
<employees>
<employee ssnum="555662222">
<first_name></first_name>
<last_name></last_name>
<contact_info>
    <office_phone></office_phone>
    <email></email>
    <cell_phone></cell_phone>
</contact_info>
<access_info level="user">
    <username>jackass</username>
    <password>letmein</password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>
<employee ssnum="555991111">
<first_name></first_name>
<last_name></last_name>
<contact_info>
    <office_phone></office_phone>
    <email></email>
    <cell_phone></cell_phone>
</contact_info>
<access_info level="user">
    <username>god</username>
    <password>qwerty</password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>
</employees>
XMLBookendMarker;

//below is the information entered by the user,
//which we will use to find the username in the
//employees.xml and then check if the password matches
$enteredUsername = 'god';
$enteredUserPass = 'qwerty';

$doc = new DOMDocument();
$doc->loadXML($xmlStr);

$selector = new DOMXpath($doc);

//the quote styles are important below
$result = $selector->query("/employees/employee/access_info[username='$enteredUsername']/password");
//if length should happen to be longer than 1, well you screwed up long
//time ago by allowing multiple same usernames, and that can't be allowed!
//if length is 0, then there is no such username
if ($result->length === 0) {
    die('NO SUCH USERNAME EXISTS');
} elseif ($result->length > 1) {
    die('ERROR: CONSULT ADMIN');
}
//ok, now we know $result->length is 1
$password = $result->item(0)->nodeValue;

if ($password == $enteredUserPass) {
    echo 'The password matches! logging in....';
    //now we might want some other info about the employee
    $employee = $result->item(0)->parentNode->parentNode;
    //just demoing showing the employee
    echo '<br><br>'.htmlentities($doc->saveXML($employee));
}

?>
astupidname
  • 1,837
  • 15
  • 11