当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP+XML 制作简单的留言本 图文教程

PHP
php ignore_user_abort与register_shutdown_function 使用方法
PHP写MySQL数据 实现代码
phpmyadmin 访问被拒绝的真实原因
PHP 程序员也要学会使用“异常”
php MYSQL 数据备份类
PHP 类型转换函数intval
php 图像函数大举例(非原创)
PHP 输出缓存详解
PHP COOKIE设置为浏览器进程
ie6 动态缩略图不显示的原因
php 将bmp图片转为jpg等其他任意格式的图片
discuz7 phpMysql操作类
一个php Mysql类 可以参考学习熟悉下
php 分页函数multi() discuz
学习discuz php 引入文件的方法DISCUZ_ROOT
php fckeditor 调用的函数
mysql 性能的检查和优化方法
PHP 高级课程笔记 面向对象
PHP 多进程 解决难题
php 404错误页面实现代码

PHP+XML 制作简单的留言本 图文教程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 143 ::
收藏到网摘: n/a

本例演示了PHP如何操纵XML。留言本具有以下基本功能.

1. 留言显示页面

2. 发布留言,并允许上传图片

3. 输入密码登录后可以删除留言。

1. 文件目录

upfile是保存上传图片的目录。

2. 主要界面

(1)首页,显示留言页面

(2)发表留言页面

3. XML文档格式,名称为data.xml

各字段的含义不多说,各元素的值看起来有点怪,是因为我使用了base64_encode对字符串进行了编码。

4 主要页面代码

(1)add.php

此页只是纯粹的HTML代码

<form action="saveadd.php" enctype="multipart/form-data" method="post" name="myform" onsubmit="return go(this)">
<table border="1" width="600">
<tr>
<td>作者</td>
<td align="left"><input type="text" name="author" size="10"></td>
</tr>
<tr>
<td>标题</td>
<td align="left"><input type="text" name="title" size="50"></td>
</tr>
<tr>
<td>表情</td>
<td align="left">
<select name="smiles" size="1" onchange="change_img();">
<option value="smile.gif">微笑</option>
<option value="biggrin.gif">耿直</option>
<option value="victory.gif">胜利</option>
<option value="tongue.gif">舌头</option>
<option value="titter.gif">窃笑</option>
<option value="cry.gif">哭泣</option>
<option value="curse.gif">生气</option>
<option value="huffy.gif">愤怒</option>
<option value="mad.gif">疯狂</option>
<option value="sad.gif">哀伤</option>
<option value="shocked.gif">震惊</option>
<option value="shy.gif">害羞</option>
<option value="sleepy.gif">困倦</option>
<option value="sweat.gif">汗</option>
</select>
<img src="smiles/smile.gif" name="img">
</td>
</tr>
<tr>
<td>内容</td>
<td align="left"><textarea name="content" cols="70" rows="10"></textarea></td>
</tr>
<tr>
<td>截图</td>
<td align="left"><input type="file" name="upfile" size="50"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>

(2)savadd.php

用于保存留言信息

<?php
if(!$_POST["author"] || !$_POST["content"])
{
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "你没有填写留言姓名或内容,2秒钟返回首页";
exit();
}else{
$imgflag=0; //用于判断是否需要上传图片
function random($length) //此函数用于生成一个随机的图片文件名(不含扩展名),以防止与现有图片重复
{
$hash = 'IMG-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) //从上面的字符串中随机找length长度个字符
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}

function fileext($filename) //此函数用于获取上传文件的扩展名
{
return substr(strrchr($filename, '.'), 1);
}

if($_FILES["upfile"]["name"]!=""){
$uploaddir="upfile/"; //图片保存路径
$type=array("jpg","gif","bmp","jpeg","png"); //允许上传的文件类型

if(!in_array(strtolower(fileext($_FILES['upfile']['name'])),$type)) //如果上传的文件的扩展名不符合要求
{
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
$text=implode(",",$type);
echo "您只能上传以下类型文件: ",$text,"<br>";
exit();
}
else
{
$filename=explode(".",$_FILES['upfile']['name']);
do
{
$filename[0]=random(10);
$randname=implode(".",$filename); //得到的最终随机生成的文件名(连同扩展名)
$uploadfile=$uploaddir.$randname;
} while(file_exists($uploadfile));

if (move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){ //保存上传的图片到upfile文件夹
echo "上传图片成功";
$imgflag=1;
}
else{
echo "上传图片失败!";
$imgflag=0;
}

}
}

//获取其他表单域

$author=base64_encode($_POST["author"]);
$content=base64_encode(ereg_replace("\r\n","<br>",$_POST["content"]));
$smiles=base64_encode($_POST["smiles"]);
if($_POST["title"]){
$title=base64_encode($_POST["title"]);
}else{
$title=base64_encode("无标题");
}
$addtime=date("Y-m-d");
if($imgflag==1){ //如果有上传图片
$photo=base64_encode($randname);
}else{ //否则将photo元素的值设置为NONE
$photo="NONE";
}
$dom=new DOMDocument('1.0','gb2312'); //指定XML的格式
$dom->load("data.xml"); //加载
$root=$dom->getElementsByTagName("messages"); //获取根节点
$root=$root->item(0);
$last_id=$root->lastChild->firstChild->nodeValue; //获取最后一个message的第一个子节点(即id节点)的值
$id=$last_id+1; //新增消息的id
settype($id,"string"); //将其转换为字符型


$message=$root->appendChild(new DOMElement('message')); //添加message节点
$el_id=$message->appendChild(new DOMElement('id')); //添加message节点的各个子节点
$el_id->appendChild($dom->createTextNode($id));

$el_author=$message->appendChild(new DOMElement('author'));
$el_author->appendChild($dom->createTextNode($author));

$el_title=$message->appendChild(new DOMElement('title'));
$el_title->appendChild($dom->createTextNode($title));

$el_smiles=$message->appendChild(new DOMElement('smiles'));
$el_smiles->appendChild($dom->createTextNode($smiles));

$el_content=$message->appendChild(new DOMElement('content'));
$el_content->appendChild($dom->createTextNode($content));

$el_addtime=$message->appendChild(new DOMElement('addtime'));
$el_addtime->appendChild($dom->createTextNode($addtime));

$el_photo=$message->appendChild(new DOMElement('photo'));
$el_photo->appendChild($dom->createTextNode($photo));

$dom->save("data.xml"); //保存XML


echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "谢谢您的留言,2秒钟返回首页";

}
?>

(3)index.php

本页面用于显示留言信息

<p><a href="add.php">添加留言</a></p>

<?php
$dom=new DOMDocument('1.0','gb2312');
$dom->load("data.xml"); //加载
$root=$dom->getElementsByTagName("messages");
$root=$root->item(0);
$message=$root->getElementsByTagName("message"); //获取所有message节点

$message_count=$message->length; //计算有多少条留言
echo "当前共有".$message_count."条留言";

if($message_count==0){
echo "暂时没有留言\n";
}else{
?>
<table border="1" width="700">
<?php
for($i=$message_count-1;$i>=0;$i--) //我们需要对留言按倒序排列
{
$msg=$message->item($i);
foreach($msg->childNodes as $child) //message节点的各个子节点
{
if($child->nodeName=="id")
{
$id=$child->nodeValue;
}
if($child->nodeName=="author")
{
$author=$child->nodeValue;
}
if($child->nodeName=="title")
{
$title=$child->nodeValue;
}
if($child->nodeName=="smiles")
{
$smiles=$child->nodeValue;
}
if($child->nodeName=="content")
{
$content=$child->nodeValue;
}
if($child->nodeName=="photo")
{
$photo=$child->nodeValue;
}
if($child->nodeName=="addtime")
{
$addtime=$child->nodeValue;
}
}
echo "<tr>";
echo "<td align=left bgcolor=#CCCCFF>";
echo $id.".<img src='smiles/".base64_decode($smiles)."'>".base64_decode($title)." - ".base64_decode($author)." [".$addtime."] ";
if(isset($_SESSION["password"]) && $_SESSION["password"]!="") //如果输入了密码显示删除链接
{
echo "[<a href='del.php?id=".$id."'>删除</a>]";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($content)."</td></tr>";
if($photo!="NONE")
{
echo "<tr><td align=left><img src='upfile/".base64_decode($photo)."'></td></tr>";
}
}
?>
<?php
}
?>
</table>
<?php
if(isset($_SESSION["password"]) && $_SESSION["password"]!=""){
?>
<p><a href="logout.php">退出管理</a></p>
<?php
}else{
?>
<p><a href="login.php">登陆管理</a></p>
<?php
}
?>

(4) 删除留言

<?php
if(isset($_SESSION["password"]) && $_SESSION["password"]!="")
{

$dom=new DOMDocument;
$dom->load("data.xml");
$root=$dom->getElementsByTagName("messages");
$root=$root->item(0);
foreach($root->childNodes as $msg)
{
if($msg->firstChild->nodeValue==$_GET["id"]) //如果message节点的id子节点的值跟要删除的id相等
{
$photo=$msg->lastChild->nodeValue;
if($photo!="NONE"){ //如果留言包含图片,还应该将图片删除
$photo_path="upfile/".base64_decode($photo);
$flag=unlink($photo_path);
if($flag){
echo "删除图片成功<br>";
}
}

$root->removeChild($msg);
break;
}
}
$dom->save("data.xml");


?>

删除留言成功,2秒钟返回首页
<meta http-equiv="refresh" content="2;url=index.php">
<?php
}else{
?>
您还未登陆,2秒钟返回登陆页面
<meta http-equiv="refresh" content="2;url=login.php">
<?php
}
?>