这是我到目前为止所拥有的,我想将日期/时间日志添加到 xml 文件中。
<?php
// Load the XML file we want to write to
$visitors = simplexml_load_file("data.xml");
// Set e-mail xml search
$search_id = htmlentities($_POST['email']);
$visitors = $visitors->xpath("visitor[email='$search_id']");
// If we have a search result, email already exists in xml file
if(isset($visitors[0])){
$visitor = $visitors[0];
$email = (string) $visitor->email;
// ********** Need to add date/time for each visit here.
// So, I guess I need to search for e-mail address, then append to it.
// Help required here... and again below to confirm formatting for multiple
// dates.
} else {
$xml = simplexml_load_file("data.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$search_id = preg_replace('/[^(\x20-\x7F)]*/','', $search_id);
$visitor = $sxe->addChild("visitor");
$visitor->addChild("email", $search_id);
// ******** Not sure this is the correct xml formatting.
$date = $visitor->addChild('date');
$date->addChild("email", date("Y-m-d H:i:s"));
$sxe->asXML("data.xml");
} // if(isset($visitors[0])){
} // if(isset($data)){ ?>
产生
<?xml version="1.0" encoding="utf-8"?>
<visitors>
<visitor>
<email>jon.doe@aol.com</email>
</visitor>
</visitors>
我想要做的是为每次访问添加一个日期日志(增加/附加)。
所以结果是(我不确定我的格式是否正确,这当然无济于事)。不要担心日期/时间的 PHP。
<?xml version="1.0" encoding="utf-8"?>
<visitors>
<visitor>
<email>jon.doe@aol.com</email>
<date>2012-11-01 11:00:00</date>
<date>2012-11-02 14:00:00</date>
</visitor>
</visitors>