php無需mysql留言本程式碼(基於xml)

來源:互聯網
上載者:User

php中操作xml文檔我們會使用SimpleXMLElement函數,我們先瞭解一下SimpleXMLElement函數用法
SimpleXML 函數允許您把 XML 轉換為對象。
通過普通的屬性選取器或數組迭代器,可以處理這個對象,就像處理任何其他對象一樣。

例子

xml文檔格式

 代碼如下 複製代碼

<?php
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("<?xml version='1.0' encoding='utf-8'?><guestbook></guestbook>");
}
if($op=='list'){
 header("Content-Type:text/html;charset=utf-8");
 if(is_object($gb)){
echo "<table>";
echo "<tr><th>ID</th><th>使用者</th><th>標題</th><th>標題</th><th>內容</th><th>時間</th><th>IP</th></tr>";
foreach($gb->item as $v){
 echo "<tr>";
 echo "<td>".htmlspecialchars($v->id)."</td><td>".htmlspecialchars($v->user)."</td><td>".htmlspecialchars($v->title)."</td><td>".htmlspecialchars($v->content)."</td><td>".date("Y-m-d H:i",intval($v->time))."</td><td>".htmlspecialchars($v->ip)."</td>";
}
echo '<table>';
 }
 echo "<div><a href='guestbook.php?op=add'>添加</a></div>";
}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'){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 <title>Document</title>
</head>
<body>
<form action="guestbook.php?op=save" method="post">
<div>使用者:<input type="text" name="user"></div>
<div>標題:<input type="text" name="title"></div>
<div>留言:<textarea name="content" id="" cols="30" rows="10"></textarea></div>
<div><input type="submit"  value="提交留言"></div>
</form>
</body>
</html>
<?php
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.