使用glob 读出所有的文件 然后使用 krsort 逆向排序 分页 读出你要留言
这是一个简单的留言本,目前还没有后台管理程序.如果哪位高手能补上,那就太好了.
演示在
留言保存在message.txt文件中,留言的格式为:date$ip$name$content
"$"为分隔符号
/****************************************
* 也就是,你使用本代码获取收益或者所以呢受
****************************************/
file: index.php
代码:
html
head
meta http-equiv="Content-Type" content="text/html; charset=gbk"
title留言板/title
link rel="stylesheet" href="../msg.css" type="text/css"
/head
body
brBFONT COLOR="#0000FF"?图片留言板/FONT/B
center
trtd style="border-right-style: none"
input class="button" type="submit" name="submit" value="发表留言"
input class="button" type="reset" value="重置" name="reset"
/form
/td/tr
/table
php
include("showmsg.php");
if(!empty($_GET['p'])){
$num=$_GET['p'];
showpage($num);
}else showpage(1);
/center
/body
/html
file: showmsg.php
function showpage($p)
{ ?
trtd
p style="line-height: 100%; margin-top: 1; margin-bottom: 1" align="left"
$num=$p;
if($num1) $num=1;
$prev=$num-1;
$next=$num+1;
$page=$num-1; //当前页码
$fname="message.txt"; //存储留言的文件
$all_msg=file($fname); //将留言读入数组
$line_count=count($all_msg);
$page_count=ceil($line_count/$perPage);
if($prev0)
echo "a href=index.php?p=$prev上一页/a?";
else
echo "上一页?";
if($line_count($next-1)*$perPage)
echo "a href=index.php?p=$next下一页/a??";
echo "下一页??";
echo "当前第 ".$num." 页,共有".$page_count."页,".$line_count."条留言.";
/p/td/tr
//显示留言
for($n=$line_count-1-$page*$perPage;$line_count-1-$page*$perPage-$n$perPage;$n--){
if(!empty($all_msg[$n])){
echo "tr bgcolor=$bg";
echo "/tr";
}
php } ?
file: savemsg.php
if (getenv("HTTP_CLIENT_IP"))
$ip= getenv("HTTP_CLIENT_IP");
elseif (getenv("HTTP_X_FORWARDED_FOR"))
$ip= getenv("HTTP_X_FORWARDED_FOR");
$ip= getenv("REMOTE_ADDR");
//获取IP地址结束
$date=date("Y年m月d日 H:i:s",time());
if(empty($_POST['guest_name']))
die("请填你的名字.a href=index.phpRefresh/a");
if(empty($_POST['guest_msg']))
die("请填写留言内容再提交.a href=index.phpRefresh/a");
$guest_name=strip_tags($_POST['guest_name']);
$guest_msg=substr($_POST['guest_msg'],0,$MSG_MAX_LEN);
//write message to file
//make the message be a line when stored
$guest_msg = str_replace( "\r\n", "\n", $guest_msg);
$guest_msg = str_replace( "\r", "\n", $guest_msg);
$guest_msg = str_replace(" "," ?",$guest_msg);
$guest_msg = str_replace("","",$guest_msg);
$guest_msg = str_replace("\'","'",$guest_msg);
//保存留言,以追加的形式
$fname="message.txt";
$fp=fopen($fname,"a+");
fwrite($fp,$date."$".$ip."$".$guest_name."$".$guest_msg."\n");
fclose($fp);
echo "meta http-equiv='refresh' content='0;url=index.php'";
用于显示效果的样式表文件
file: msg.css
A:link {
text-decoration: none;
A:visited {
A:hover {
text-decoration: underline;
A:active {
BODY{
font-family: Verdana,Arial,Helvetica,sans-serif;
TABLE{
border-collapse: collapse;
table-layout: fixed;
margin: 0px;
input.htm
meta http-equiv="Content-Language" content="zh-cn"
titleinput/title
form method="POST" action="receive.php"
p您的性别:男input type="radio" value="0" name="R1"??
女input type="radio" name="R1" value="1"/p
p您的留言内容:/p
p /p
p?input type="submit" value="提交" name="B1"??
receive.php
$user='root';
$db='guestbook';
$table='gbook';
$ip=getenv(REMOTE_ADDR);
$connect=mysql_connect('localhost',$user,$password);
mysql_select_db($db);
mysql_query($sql);
$result=mysql_query("select * from $table");
while ($arr=mysql_fetch_array($result))
{
$gender='先生';
$gender='女士';
titleReceive/title
body style="background-attachment: fixed"
table border="1" width="100%" id="table1" bgcolor="#FFFFFF"
tr
写到:/td
/tr
pa href="del.php?id=?=$arr[0]?"[删除]/a??
a href="modify.php?id=?=$arr[0]?"[修改]/a]/td
echo 'p';
title新建网页 1/title
pa href="input.htm"继续留言/a/p
del.php
$sql="DELETE FROM $table WHERE id=$id";
$result=mysql_query($sql);
if ($result)
echo "删除成功";
echo "删除失败";
pa href="receive.php"返回首页/a/p
modify.php
$result=mysql_query("select * from $table where id=$id");
$arr=mysql_fetch_array($result);
form method="POST" action="modify_ok.php?id=?=$id?"
p您的性别:
女input type="radio" name="R1" value="1"/p';
else echo '男input type="radio" value="0" name="R1"??
女input type="radio" name="R1" value="1" checked/p';
p?input type="submit" value="修改" name="B1"??
modify_ok.php
mysql_select_db($db);;
echo "修改成功";
echo "修改失败";
php是脚本语言,没有数据库,可能还真没办法.
以前倒是有人用xml做过一个不用数据库的留言本,但那可不是实时聊天的哦.
数据库的创建
CREATE DATABASE GBOOK;
CREATE TABLE +gbook+ (
+name+ VARCHAR( 10 ) NOT NULL ,
+sex+ TINYINT( 1 ) UNSIGNED NOT NULL ,
+info+ TEXT NOT NULL ,
+time_at+ DATETIME NOT NULL
)
主要页面有:
input.html为初始页
insert.php为把输入内容送入数据库
show.php显示留言板内容
change.php修改留言板内容的界面
del.php删除留言板内容
change_ok.php修改留言板内容并送入数据库
input.html
title留言版/title
form name="form1" method="post" action="insert.php"
p你的性别:input type="radio" value="1" name="sex" checked="checked" /男生 input type="radio" value="0" name="sex" /女生 /p
p你的留言内容:/p
insert.php
$mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_database = "gbook";
$ip = getenv('REMOTE_ADDR');
mysql_select_db("gbook");
$sql = "INSERT INTO +gbook+ ( +id+ , +name+ , +sex+ , +email+ , +info+ , +ip+ , +time_at+ )
VALUES (NULL , '$name', '$sex', '$email', '$info', '$ip', NOW( ))";
$result = mysql_query($sql,$conn);
$id = mysql_insert_id();
mysql_close($conn);
p留言成功/p
pa href="show.php"去留言页 /a/p
show.php
$sql = "SELECT * FROM gbook ORDER BY 'id' DESC"; //排序 后留言的在前面显示
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{ // ----if语句判断男女------
{ $gender = '男';}
{ $gender = '女'; }
hr /
mysql_free_result($result);
change.php
$sql = "SELECT name, sex, email, info FROM gbook WHERE id = '$id' ";
$row = mysql_fetch_row($result);
form name="form1" method="post" action="change_ok.php?id?=$id?"
// -----if语句判断男女 1为男 0为女-------
if($row[1]==1)
echo ' p你的性别:input type="radio" value="1" name="sex" checked="checked" /男 input type="radio" value="0" name="sex" /女 /p';
echo ' p你的性别:input type="radio" value="1" name="sex" /男 input type="radio" value="0" name="sex" checked="checked" /女 /p';
pinput type="submit" value="提交" /input type="reset" value="重设" //p
$sql = "DELETE FROM gbook WHERE id = '$id' ";
pa href="show.php"[返回]/a/p
change_ok.php
$sql = "UPDATE +gbook+ SET +name+ = '$name',+sex+ = '$sex',+email+ = '$email',+info+ = '$info' WHERE +id+ ='$id' ";
留言板也就需要两个页面
一个是前台用户填写留言用的 是几个文本框 姓名 联系方式 留言内容等 用户填好提交以后点提交 就把这些数据插入到数据库
另一个是后台管理员查看留言的页面
也就是把数据库里的留言读出来 展示到页面上
再设置几个操作按钮 比如删除留言
以上就是土嘎嘎小编为大家整理的php无数据库留言板源码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!