跟我一起学写上升级单文件留言本
原创单文件留言板 gbk版
看了视频教程之后写的留言版
基本涵盖了大部分上升级基础知识点,变量,常量,循环(if,switch),变量作用域,session,函数,递归函数,安全处理,类,构造函数,静态变量,数据库连接、增加、查询、更新、删除
html的知识点 隐藏表单 js表单验证 页面定时重定向 css悬浮
实现功能:
增加留言 留言删除 翻页 管理员修改密码 基于一个页面实现留言板的完整功能
注释比较详细
用户名:admin
密码:123456
<?上升级
session_start(); //启动会话
/*
* 单文件留言本
* 请注明来自于 PHP100.com
*/
//变量 定义、赋值
$db_host = 'localhost:3306'; //设置数据库主机端口号 一般为localhost:3360 通常不需修改
$db_user = 'root'; //设置数据库用户名
$db_pass = 'liujie'; //设置数据库密码
$db_name = 'test'; //设置数据库名称
$db_char = 'GBK'; //设置数据库字符集 默认GBK 通常情况不需修改
//定义常量
define('Per_page',6); //定义每页记录数
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://http://www.zjjv.com///TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://http://www.zjjv.com///1999/xhtml" xml:lang="zh" lang="zh" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>k 留言板</title>
<style>
<!--
body{padding:0;margin:0;height:100%;overflow-y:auto;}
#contain{width:900px;margin:0 auto;}
#hd{width:400px;margin:50px auto;}
.forms{margin:60px auto;width:400px;color:green;font-size:20px;}
.list{width:900px;margin:20px auto;}
.current{color:green;font-size:20px;}
#showmsg {display:none; top:100px; left:300px; width:600px; height:300px;position:fixed;background:#CCF;padding:50px;opacity: 0.9;} /* IE并不认识fixed,而FF认识 */
* html #showmsg {position:absolute;} /* 这个只有IE认识 */
-->
</style>
<script language="javascript">
<!--
//表单验证
function CheckForm(){
if(myform.username.value==""){
alert("请填写用户名");
myform.username.focus();
return false;
}
if(myform.title.value==""){
alert("请填写标题");
myform.title.focus();
return false;
}
if(myform.msg.value.length<5){
alert("留言需要大于5个字符");
myform.msg.focus();
return false;
}
}
function CheckPassword(){
if(repassform.admin_pass.value.length<6){
alert("密码长度大于6");
repassform.admin_pass.focus();
return false;
}
if(repassform.admin_pass.value != repassform.admin_pass2.value){
alert("密码不相同");
repassform.admin_pass2.focus();
return false;
}
}
//全选
function checkAll(name)
{
var el = document.getElementsByTagName('input');
var len = el.length;
for(var i=0; i<len; i++)
{
if((el[i].type=="checkbox") && (el[i].name==name))
{
el[i].checked = true;
}
}
}
//取消全选
function clearAll(name)
{
var el = document.getElementsByTagName('input');
var len = el.length;
for(var i=0; i<len; i++)
{
if((el[i].type=="checkbox") && (el[i].name==name))
{
el[i].checked = false;
}
}
}
//反选
function disAll(name)
{
var el = document.getElementsByTagName('input');
var len = el.length;
for(var i=0; i<len; i++)
{
if((el[i].type=="checkbox") && (el[i].name==name) && el[i].checked == true)
{
el[i].checked = false;
}else{
el[i].checked = true;
}
}
}
//-->
</script>
</head>
<body>
<div id="contain">
<div id="hd"><h1>留言板</h1><a href="?">首页</a> <a href="?do=manage">管理</a></div>
<div id="showmsg">
<p id="info" name="info"></p>
<p id="tip" name="tip"></p>
</div>
<script language="javascript">
<!--
//跳转
function Redirect(Url){
window.location = Url;
}
var i = 0;
//显示倒计时
function dis(time){
document.getElementById("tip").innerHTML = "" + (time - i) + "秒后跳转";
i++;
}
//显示提示,定时跳转
function ShowMsg(msg,time,Url){
document.getElementById("showmsg").style.display= "block" ;
document.getElementById("info").innerHTML = msg;
if(time>0 && Url != ''){
timer=setInterval("dis('"+time+"')", 1000);//显示时间
timer=setTimeout("Redirect('"+Url+"')",time * 1000); //跳转
}
}
//-->
</script>
<?上升级
//实例化基础类
$base = new base($db_host,$db_user,$db_pass,$db_name,$db_char);
//定义基础类
class base{
private static $conn ;
var $err ;
//上升级5 构造函数 初始化连接
function __construct($db_host,$db_user,$db_pass,$db_name,$db_char='GBK'){
/*连接数据库& 设置数据库字符集*/
@ $dbo = mysql_connect($db_host,$db_user,$db_pass) or die('请检查是否数据库密码是否正确。');
mysql_select_db($db_name,$dbo) or die ('数据库不存在,请确认是否正确安装');
mysql_query("set names '$db_char'",$dbo);
$this->conn = $dbo;
}
//上升级4 构造函数
function base($db_host,$db_user,$db_pass,$db_name,$db_char='GBK'){
$this->__construct($db_host,$db_user,$db_pass,$db_name,$db_char);
}
//增加留言
function add(&$data){
$username = $data['username']?$data['username']:'匿名';
$title = $data['title'];
$msg = $data['msg'];
$sql = sprintf("insert into `liuyan` values('','%s','%s','%s','%s')",$username,$title,$msg,time());
mysql_query($sql,$this->conn);
return mysql_insert_id($this->conn);
}
//留言列表
function lists($p=0){
$c = 0;
if($p>0)$c=($p-1) * Per_page;
$limit = " limit ".$c.",".Per_page;
$where = '1';
$r = '';
$sql = sprintf("SELECT * from `liuyan` where '%s' $limit",$where.$limit);
$query = mysql_query($sql,$this->conn);
while($row = mysql_fetch_array($query)){
$r[] = $row;
}
return $r;
}
//统计分页
function page(){
$sql = "select count(id) from `liuyan`";
$query = mysql_query($sql,$this->conn);
$row = mysql_fetch_row($query);
$page['totals'] = $row[0];
$page['nums'] = ceil($row[0]/Per_page);
return $page;
}
//删除留言
function del($id){
$this->check();
if(is_array($id)){
$ids .= implode(',',$id);
}else{
$ids = $id;
}
$sql = "delete from `liuyan` where id in($ids)";
mysql_query($sql,$this->conn);
return mysql_affected_rows($this->conn);
}
//登录
function login($admin_name,$admin_pass){
$pass = md5($admin_name.$admin_pass.$admin_name);
$sql="select * from `admin` where `name`='$admin_name' and `pass` = '$pass' ";
$query = mysql_query($sql,$this->conn);
$row = mysql_fetch_array($query);
if($row['id']>0){
$_SESSION=$row;
ShowMsg('登录成功,欢迎管理员'.$_SESSION['name'],3,'?do=manage');
}else{
ShowMsg('登录失败'.$_SESSION['name'],3,'?do=login');
}
}
//密码修改
function repass($admin_name,$admin_pass,$admin_id){
$this->check();
$pass = md5($admin_name.$admin_pass.$admin_name);
$sql = "UPDATE `admin` set name='$admin_name' , pass='$pass' where id=$admin_id";
mysql_query($sql,$this->conn);
return mysql_affected_rows($this->conn);
}
//退出
function logout(){
$this->check();
session_destroy();
}
//检查是否登录
function check(){
if($_SESSION['name'] && $_SESSION['pass']){
//ShowMsg('登录成功,欢迎管理员'.$_SESSION['name'],3,'?do=manage');
}else{
ShowMsg('请先登录'.$_SESSION['name'],3,'?do=login');
}
}
function getErr(){
return $this->err;
}
function setErr(){
$this->err = mysql_error($this->conn);
}
}
//安全处理 递归函数
function Post_check($arr){
if(is_array($arr)){
foreach($arr as $n=>$k){
$data[$n] = Post_check($k);
}
}else{
return addslashes(trim($arr));
}
return $data;
}
//定时跳转
function ShowMsg($msg,$time=0,$url=''){
echo "<script>ShowMsg('$msg',$time,'$url');</script>";
exit;
}
?>
<?上升级
//流程处理 不同操作显示不同界面
$do = $_REQUEST['do'];
switch($do){
//添加留言
case 'add':
if($_POST['do'] == 'add'){
$data = Post_check($_POST);
if(@$base->add($data)>1){
echo '留言添加成功';
}else{
echo $base->err;
}
}
break;
//删除留言
case 'del':
$data = Post_check($_POST);
$affected_rows=$base->del($data['id']);
if($affected_rows>0){
echo '成功删除'.$affected_rows.'行'.'<a href="?do=manage">返回</a>';
}else{
echo '删除失败';
}
break;
//管理员修改密码
case 'repass':
$data = Post_check($_POST);
$r = $base->repass($data['name'],$data['admin_pass'],$data['id']);
if($r){
echo '密码修改成功';
}else{
echo '密码修改失败';
}
break;
//管理员退出登录
case 'logout':
$base->logout();
ShowMsg('成功退出登录'.$_SESSION['name'],3,'?do=');
break;
//管理员登录
case 'login':
if($_POST['admin_name'] && $_POST['admin_pass']){
$data = Post_check($_POST);
$base->login($data['admin_name'],$data['admin_pass']);
}
?>
<!--登录部分-->
<div class="forms" >
<table>
<form action="" method="post" name="myform" id="myform" onsubmit="return CheckForm();">
<tr><td><label for = "admin_name">管理员:</label><input type="text" id="admin_name" name="admin_name"/></td></tr>
<tr><td> </td></tr>
<tr>
<td>
<label for = "admin_pass">密 码:</label><input type="password" id="admin_pass" name="admin_pass"/></td></tr>
<tr><td> </td></tr>
<tr><td colspan="2">
<input type= "submit" value = "登录" />
</form>
</td></tr></table>
</div>
<!--登录部分-->
<?上升级
//管理留言
break;
case 'manage':
$base->check();
$p = intval($_GET['p']);
$lists = $base ->lists($p);
if(!$lists){
echo '留言为空';
}else{
?>
<!--修改密码-->
<div class='admin'>
<form action="" method="post" name="repassform" id="repassform" onsubmit="return CheckPassword();">
<input type = "hidden" id ="do" name = "do" value = "repass" />
<input type = "hidden" id ="id" name = "id" value = "<?上升级 echo $_SESSION['id'];?>" />
<input type = "hidden" id ="name" name = "name" value = "<?上升级 echo $_SESSION['name'];?>" />
<table>
<tr><td>管理员:<?上升级 echo $_SESSION['name'];?></td><td> <a href="?do=logout">退出</a> </td><td>新密码:<input type="password" id="admin_pass" name="admin_pass"/></td><td> </td><td>重复密码:<input type="password" id="admin_pass2" name="admin_pass2"/></td><td> </td><td><input type= "submit" value = "修改密码" /></td></tr>
</table>
</form>
</div>
<!--修改密码-->
<!--管理列表-->
<div class="list">
<form action="" method="post">
<input type = "hidden" id ="do" name = "do" value = "del" />
<table width="880px">
<tr>
<td width="5%">选择</td><td width="5%">id</td><td width="15%">name</td><td width="15%">title</td><td width="40%">msg</td><td width="15%">time</td>
</tr>
<?上升级 foreach($lists as $row){
?>
<tr><td><input type="checkbox" name="id[]" id="id[]" value="<?上升级 echo $row['id']?>"/></td><td><?上升级 echo $row['id'];?></td><td><?上升级 echo $row['name'];?></td><td><?上升级 echo $row['title'];?></td><td><?上升级 echo $row['msg'];?></td><td><?上升级 echo date("Y-m-d",$row['addtime']);?></td></tr>
<?上升级
}
?>
<tr>
<td width="5%"><input type="button" value="全选" onclick="checkAll('id[]')" /></td><td width="5%"><input type="button" value="取消" onclick="clearAll('id[]')" /></td><td width="5%"><input type="button" value="反选" onclick="disAll('id[]')" /></td><td width="5%"><input type="submit" value="删除"/></a></td>
</tr>
</table>
</form>
<?PHP
}
//分页
$baseurl = '?do=manage&p=';
$pages = $base->page();
echo '共'.$pages['totals'].'条 '.$p.'/'.$pages['nums'].'页';
echo "<a style='margin:0 10px;' href='".$baseurl."0' target='_self'>首页</a>";
for($i=0;$i<3;$i++){
$class='';
$link = 1;
if($p-1>0)$link =$p-1;
$link+=$i;
if($link>$pages['nums'])break;
if($link==$p){
$class="class='current'";
}
echo "<a style='margin:0 10px;' $class href='".$baseurl.$link."' target='_self'>".$link."</a>";
}
echo "<a style='margin:0 10px;' href='".$baseurl.$pages[nums]."' target='_self'>末页</a>";
?>
</div>
<!--管理列表-->
<?上升级
break;
?>
<?上升级
//默认显示留言列表
default :
$p = intval($_GET['p']);
$lists = $base ->lists($p);
if(!$lists){
echo '留言为空';
}else{
?>
<div class="list">
<!--留言列表-->
<table width="880px">
<tr>
<td width="5%">id</td><td width="15%">name</td><td width="15%">title</td><td width="50%">msg</td><td width="15%">time</td>
</tr>
<?上升级 foreach($lists as $row){
?>
<tr><td><?上升级 echo $row['id'];?></td><td><?上升级 echo $row['name'];?></td><td><?上升级 echo $row['title'];?></td><td><?上升级 echo $row['msg'];?></td><td><?上升级 echo date("Y-m-d",$row['addtime']);?></td></tr>
<?上升级
}
?>
</table>
<!--留言列表-->
<?PHP
}
//分页部分
$pages = $base->page();
echo '共'.$pages['totals'].'条 '.$p.'/'.$pages['nums'].'页';
echo "<a style='margin:0 10px;' href='?p=0' target='_self'>首页</a>";
//显示当前页,上一页 下一页 并自定义样式
for($i=0;$i<3;$i++){
$class='';
$link = 1;
if($p-1>0)$link =$p-1;
$link+=$i;
if($link>$pages['nums'])break;
if($link==$p){
$class="class='current'";
}
echo "<a style='margin:0 10px;' $class href='?p=$link' target='_self'>".$link."</a>";
}
echo "<a style='margin:0 10px;' href='?p=$pages[nums]' target='_self'>末页</a>";
?>
</div>
<div class="forms" >
<form action="" method="post" name="myform" id="myform" onsubmit="return CheckForm();">
<input type = "hidden"id ="do" name = "do" value = "add" />
<label for = "username">用户</label><input id="username" name="username"/><br/>
<label for = "title">标题</label><input id="title" name="title"/><br/>
<label for = "msg">信息</label><textarea id="msg" name="msg"></textarea>
<input type= "submit" value = "提交" />
</form>
</div>
<?上升级
break;
}?>
</div>
</body>
</html>
来自 http://http://www.zjjv.com///blog/15.html