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.

4

3 回答 3

2

似乎你被告知胡说八道DOMDocument。或者——更有可能——你误解了一些东西。您将不会使用 mysql 查询,您将使用xpath 查询来访问 xml 中的节点。您确实应该使用DOMDocumentandDOMXPath来访问节点。

下面是一个如何读取或修改密码的示例:

$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');

如果您需要有关 XPath 的更多信息,可以按照本教程进行操作。

于 2013-04-21T22:26:33.253 回答
0

我希望有几件事可以帮助您理清思路,并扩展 hek2mgl 的良好和适当的建议。我意识到它正在学习等等,但是如果正确完成,您根本不需要循环(当然假设您只需要访问一个当前试图登录的员工)。在 hek2mgl 链接到的教程中,它向您展示了谓词的全部内容,您应该使用一个在 xpath 查询中立即通过用户输入的用户名自动选择正确的员工,然后将密码与密码进行比较由用户输入并查看它们是否匹配。这样就不需要知道任何关于 $result 长度的信息(当然,它的有效长度为 1 除外)。假设包含所有员工元素的 xml 文件有一个名为“员工”的根容器元素

<?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));
}

?>
于 2013-04-22T00:03:52.780 回答
0

我的第一个想法是使用 xml_parse ,或者按照Jon的建议,在 PHP 手册中也使用 SimpleXML示例,请参阅这个问题

于 2013-04-21T23:15:02.187 回答