php无需mysql留言本程序代码(基于xml)

作者:袖梨 2022-06-24

php中操作xml文档我们会使用SimpleXMLElement函数,我们先了解一下SimpleXMLElement函数用法
SimpleXML 函数允许您把 XML 转换为对象。
通过普通的属性选择器或数组迭代器,可以处理这个对象,就像处理任何其他对象一样。

例子

xml文档格式

代码如下 复制代码

error_reporting(E_ALL ^ E_NOTICE);
$op=$_GET['op'];
$op || $op='list';

$filename='guestbook.xml';
if(is_file($filename)){
$gb=simplexml_load_file($filename);
}else{
$gb=new SimpleXMLElement("");
}
if($op=='list'){
header("Content-Type:text/html;charset=utf-8");
if(is_object($gb)){
echo "

";
echo "";
foreach($gb->item as $v){
echo "";
echo "";
}
echo '
ID 用户 标题 标题 内容 时间 IP
".htmlspecialchars($v->id)." ".htmlspecialchars($v->user)." ".htmlspecialchars($v->title)." ".htmlspecialchars($v->content)." ".date("Y-m-d H:i",intval($v->time))." ".htmlspecialchars($v->ip)."
';
}
echo "
";
}elseif($op=='save'){
if(@$_POST['user']){
$user=$_POST['user'];
$title=$_POST['title'];
$content=$_POST['content'];

/*
$id=@count($gb->item);
$nextid=$id+1;
*/
$nextid=1;
foreach($gb->item as $v){
$idarr[]=(int)$v->id;
}
$nextid=max($idarr)+1;
$item=$gb->addChild('item');
$item->addChild("id",$nextid);
$item->addChild('user',$user);
$item->addChild('title',$title);
$item->addChild('content',$content);
$item->addChild('time',time());
$item->addChild('ip',$_SERVER['REMOTE_ADDR']);
$gb->asXML($filename);
//跳转页,中间页
header("Location: guestbook.php?op=list");
die;
}
}elseif($op=='add'){
?>




Document



用户:

标题:

留言:textarea name="content" id="" cols="30" rows="10">





}
?>

相关文章

精彩推荐