XPATHとPHPは初めてです。PHPのXPATHを使用して、ユーザー検証を実現しようとしています。私のデータはxmlファイルにあります
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<id>1</id>
<fName>sa</fName>
<lastName>aa</lastName>
<email>[email protected]</email>
<password>a</password>
</customer>
<customer>
<id>2</id>
<fName>bb</fName>
<lastName>cc</lastName>
<email>[email protected]</email>
<password>b</password>
</customer>
</customers>
私のphpコードスニペットは
if(file_exists('customer.xml'))
{
$doc = new DOMDocument();
$doc->load('customer.xml');
$xpathvar = new Domxpath($doc);
//check if user exists and password matches
$queryResult = $xpathvar->query("customers/customer[email= '".$userEmail."' and password= '".$password."']");
var_dump($queryResult);
if(count($queryResult)== 1)
{
//successful login
echo "great";
}
else
{
echo "Invalid email address or password";
}
}
どの入力を提供しても、count($queryResult)
1を返します。正しいマッチングが行われない理由がわかりません。
私が提供した場合$userEmail="[email protected]"
と、次の出力が得られます$password="a"
var_dump($queryResult);
object(DOMNodeList)[3]
public 'length' => int 0
$ queryResultはDOMNodeListオブジェクトであり、プロパティの長さは見つかったノードの量です。を使用したテスト結果:
if ($queryResult->length == 1)
そして、ルート要素が表示されないため、Xpathを変更します。
customer[email= '".$userEmail."' and password= '".$password."']
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加