基本涵盖了大部分php基础知识点,变量,常量,循环(if,switch),变量作用域,session,函数,递归函数,安全处理,类,构造函数,静态变量,数据库连接、增加、查询、更新、删除
//实例化基础类
$base = new base($db_host,$db_user,$db_pass,$db_name,$db_char);
//定义基础类
class base{
private static $conn ;
var $err ;
//php5 构造函数 初始化连接
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;
}
//php4 构造函数
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 "";
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.'行'.'返回';
}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']);
}
?>
//管理留言
break;
case 'manage':
$base->check();
$p = intval($_GET['p']);
$lists = $base ->lists($p);
if(!$lists){
echo '留言为空';
}else{
?>
}
//分页
$baseurl = '?do=manage&p=';
$pages = $base->page();
echo '共'.$pages['totals'].'条 '.$p.'/'.$pages['nums'].'页';
echo "首页";
for($i=0;$i<3;$i++){
$class=&#39;&#39;;
$link = 1;
if($p-1>0)$link =$p-1;
$link+=$i;
if($link>$pages[&#39;nums&#39;])break;
if($link==$p){
$class="class=&#39;current&#39;";
}
echo "".$link."";
}
echo "末页";
?>
break;
?>
//默认显示留言列表
default :
$p = intval($_GET[&#39;p&#39;]);
$lists = $base ->lists($p);
if(!$lists){
echo &#39;留言为空&#39;;
}else{
?>
id | name | title | msg | time |
?>
| | | | |
}
?>
}
//分页部分
$pages = $base->page();
echo &#39;共&#39;.$pages[&#39;totals&#39;].&#39;条 &#39;.$p.&#39;/&#39;.$pages[&#39;nums&#39;].&#39;页&#39;;
echo "首页";
//显示当前页,上一页 下一页 并自定义样式
for($i=0;$i<3;$i++){
$class=&#39;&#39;;
$link = 1;
if($p-1>0)$link =$p-1;
$link+=$i;
if($link>$pages[&#39;nums&#39;])break;
if($link==$p){
$class="class=&#39;current&#39;";
}
echo "".$link."";
}
echo "末页";
?>
break;
}?>