Home > Article > Backend Development > Development of Micro News System
"You can only know the depth after experience." The purpose of developing this system is mainly to learn, streamline the system development process, and further combine theory with practical application. This is also phpAn important milestone in learning entry-level.
Development environment: Apache2.0+php5.4+mysql5.5
Development tools: textEditor(dreamweaver/editplus)
1.Smarty application
2.Backend membership, permission verification
3.Object, class encapsulation
1.MySQL database
There are mainly 5 data tables: full site configuration tablep_config
, backend member tablep_admin
, news classification tablep_newsclass
, news title tablep_newsbase
, News content table p_newscontent
, as shown below:
2. Program FileWebsite
Directory structureList, as shown in the figure:
##Website directory structure
):1.mysql
Creation of databaseThe configuration diagrams of each table are pasted in turn:
p_config table
##p_admin table
##p_newsclass table
Detailed explanation:
These three There is a relationship between the tables:
p_newscontent|nid --> p_newsbase|id
p_newsclass
is a top-level column when
means that the category belongs to a subcategory of the category with id 1. 2.smarty configuration
(1). Set up the config.php configuration file , the source code is as follows:
//数据库常用变量配置 $myhost ="localhost"; //主机名 $mydbuser ="root"; //数据库用户名 $mydbpw ="password"; //数据库密码 $mydbname ="news_system"; //数据库名称 $mydbcharset ="GBK"; //数据库编码 //smarty常用变量配置 $smarty_template_dir ='./templates/'; //模板路径变量 $smarty_compile_dir ='./templates_c/'; //编译目录模板变量 $smarty_config_dir ='./configs/'; //配置目录变量 $smarty_cache_dir ='./cache/'; //缓存目录变量 $smarty_caching ='false'; //缓存开关变量 $smarty_delimiter =explode("|","{|}"); //定界符变量,返回数组array([0]=>'{',[1]=>'}') ?>(2). Set up the global global call file, the source code is as follows:
template_dir = $smarty_template_dir; //模板目录 $smarty->compile_dir = $smarty_compile_dir; //编译目录 $smarty->config_dir = $smarty_config_dir; //配置目录 $smarty->cache_dir = $smarty_cache_dir; //缓存目录 $smarty->caching = $smarty_caching; //缓存开关 $smarty->left_delimiter = $smarty_delimiter[0]; //左定界符 $smarty->right_delimiter = $smarty_delimiter[1]; //右定界符 $smarty->assign("t_dir",$smarty_template_dir); //模板路径变量映射 ?>
db_host=$db_host; $this->db_user=$db_user; $this->db_pwd=$db_pwd; $this->db_database=$db_database; $this->conn=$conn; $this->coding=$coding; $this->connect(); } //连接数据库 public function connect(){ if ($this->conn == "pconn") { //永久链接 $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); } else { //即时链接 $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); } if (!mysql_select_db($this->db_database, $this->conn)) { if ($this->show_error) { $this->show_error("数据库不可用:", $this->db_database); } } mysql_query("set names $this->coding"); } //数据库执行 public function query($sql){ if($sql=""){ $this->show_error("SQL语句错误:","SQL查询语句为空"); } $this->sql=$sql; $result=mysql_query($this->sql,$this->conn); if (!$result) { //调试中使用,sql语句出错时会自动打印出来 if ($this->show_error) { $this->show_error("错误SQL语句:", $this->sql); } } else { $this->result = $result; } } //取得记录值,获取数组-索引和关联 public function fetch_array(){ return mysql_fetch_array($this->result); } //简化查询 public function select($table,$columnName="*",$condition='',$debug=''){ $condition=$condition ? "where" . $condition : NULL; if($debug){ echo "select $columnName from $table $condition"; }else{ $this->query("select $columnName from $table $condition"); } } //简化查询select public function findall($table){ $this->query("select * from $table"); } //取得上一步 INSERT 操作产生的 public function insert_id() { return mysql_insert_id(); } //错误记录 public function show_error($message="",$sql=""){ if(!$sql){ echo "" . $message . ""; echo ""; echo "
"; }else{ echo "
4. Backend login and user permission judgmentBackground login and user permission judgment are mainly implemented through
session
Quote is enough. (1). First, we need to create the background call file admin_global.php. The source code is as follows:
select('p_admin',"*",'`uid`=\''.$uid.'\''); $us=is_array($row=$this->fetch_array($query)); $ps=$us ? $shell=md5($row[username].$row[password]."TKBK") : FALSE; return $shell ? $row : NULL; } public function Get_user_shell_check($uid,$shell,$m_id=9){ if($row=$this->Get_user_shell($uid,$shell)){ if($row[m_id] <= $m_id){ return $row; }else{ echo "你无权限操作!"; exit(); } }else{ $this->Get_admin_msg('index.php','请先登录'); } } //=================================== //用户登录时间超时 public function Get_user_ontime($long='3600'){ $new_time=mktime(); $onlinetime=$_SESSION[ontime]; if($new_time-$onlinetime>$long){ echo "登录超时"; session_destroy(); exit(); }else{ $_SESSION[ontime]=mktime(); } } //用户退出登录 public function Get_user_out(){ session_destroy(); $this->Get_admin_msg('index.php','退出成功'); } //用户登录 public function Get_user_login($username,$password){ $username=str_replace(" ","",$username); //过滤提交上来username中的空格 $query=$this->select('p_admin','*','`username` = \''.$username.'\'');//调用查询方法,查询管理员表中的username $us=is_array($row=$db->fetch_array($query)); $ps=$us ? md5($password)==$row[password] : FALSE; if($ps){ $_SESSION[uid]=$row[uid]; $_SESSION[shell]=md5($row[username].$row[password]."TKBK"); $_SESSION[ontime]=mktime(); $this->Get_admin_msg('main.php','登录成功!'); }else{ $this->Get_admin_msg('index.php','密码或用户错误!'); session_destroy(); } } /** * 后台通用信息提示 */ public function Get_admin_msg($url, $show = '操作已成功!') { $msg = '管理区域
信息提示 |
---|
' . $show . ' |
Get_user_login($_POST[username],$_POST[password]); } ?>后台管理
到此已实现用户的后台登录验证,用户权限的判断只需将以下代码粘贴到每个后台页面即可:
include_once('admin_global.php'); $r=$db->Get_user_shell_check($uid,$shell);
(4).后台管理页面:
后台全局调用页面admin_global.php
后台首页main.php
左侧导航页面admin_left.php
网站参数配置页面admin_main.php
新闻栏目分类管理页面admin_news_class.php
新闻列表页面admin_news_list.php
新闻编辑添加页面admin_news_add.php
各个页面源码如下:
admin_global.php:引入各类文件
main.php:利用frame标签引入各个功能页面
网站后台控制面板
admin_left.php:后台左侧导航
Get_user_shell_check($uid,$shell); ?>PHP100_left
≡ 基础操作 ≡
配置信息 退出后台
≡ 新闻内容 ≡
新闻分类 新闻列表 添加新闻
〓 版本信息 〓 PHP100news 1.0 PHP100.com
admin_main.php:网站参数配置页面
Get_user_shell_check($uid, $shell); if($_GET[action]=='logout')$db->Get_user_out(); $query=$db->findall("p_config"); while($row=$db->fetch_array($query)){ $row_arr[$row[name]]=$row[values]; } if(isset($_POST['update'])){ unset($_POST['update']); foreach($_POST as $name=>$values){ $db->query("update p_config set `values`='$values' where `name`='$name'"); } $db->Get_admin_msg("admin_main.php"); } ?>后台管理
系统配置 | |
---|---|
网站名称: | |
网站地址: | |
关键字: | |
说明: | |
电话: | |
email: | |
admin_news_class.php:新闻栏目分类管理页面
Get_user_shell_check($uid,$shell); if(isset($_POST['into_class'])){ $db->query("INSERT INTO `p_newsclass` (`id`,`f_id`,`name`,`keywrod`,`remark`) VALUES (NULL,'$_POST[f_id]','$_POST[name]','','')"); $db->Get_admin_msg("admin_news_class.php","已经成功添加分类"); } if(!empty($_GET[del])){ $db->query("DELETE FROM `p_newsclass` WHERE `id` = '$_GET[del]' LIMIT 1;"); $db->Get_admin_msg("admin_news_class.php","删除成功"); } if(isset($_POST[update_class])){ $db->query("update `p_newsclass` set `name` = '$_POST[name]' WHERE `id` = '$_POST[id]' LIMIT 1;"); $db->Get_admin_msg("admin_news_class.php","更新成功"); } ?>后台管理
后台 >> 新闻分类 |
---|
添加分类 |
---|
系统分类 |
---|
admin_news_list.php:新闻列表页面
Get_user_shell_check($uid,$shell); $query=$db->findall("p_newsclass"); while($row=$db->fetch_array($query)){ $news_class_arr[$row[id]]=$row[name]; } if(!empty($_GET[del])){ $db->query("DELETE FROM `p_newsbase` WHERE `id` = '$_GET[del]'"); $db->query("DELETE FROM `p_newscontent` WHERE `nid` = '$_GET[del]' LIMIT 1;"); $db->Get_admin_msg("admin_news_list.php","删除成功"); } ?>后台管理
后台 >> 新闻管理 |
---|
新闻分类 | 新闻标题 | 作者 | 日期 | 操作 | '>删除 / '>修改 |
---|---|---|---|---|
admin_news_add.php:新闻编辑添加页面
Get_user_shell_check($uid,$shell); if(isset($_POST[into_news])){ $db->query("insert into `p_newsbase` (`id`,`cid`,`title`,`author`,`date_time`)" . "values (NULL,'$_POST[cid]','$_POST[title]','$_POST[author]','".mktime()."')"); $last_id=$db->insert_id(); $db->query("insert into `p_newscontent` (`nid`,`keywrod`,`content`,`remark`)" . "values ('$last_id','$_POST[keywrod]','$_POST[content]','')"); $db->Get_admin_msg("admin_news_add.php","添加成功"); } ?>后台管理
后台 >> 添加新闻 |
---|
添加分类 | |
---|---|
新闻分类 | |
新闻标题 | |
新闻作者 | |
新闻关键字 | |
新闻内容 | |
(5)分页功能page.class.php
后台和前台分别调用了不同的分页函数,以免混淆,不过由于函数功能基本不一样,这里只贴出前台的分页函数源码:
首页 "; }else{ $pagenav.="首页 "; } if($page==1){ $pagenav.=" 上一页 "; }else{ $pagenav.="上一页 "; } for($i=1;$i<=$lastpg;$i++){ if($i==$page) $pagenav.=" $i "; else $pagenav.=" $i "; } if($page==$lastpage){ $pagenav.=" 下一页 "; }else{ $pagenav.="下一页 "; } if($page==$lastpg){ $pagenav.=" 末页 "; }else{ $pagenav.="末页 "; } } } ?>
(6)前台首页
包括首页程序文件index.php,smarty模板文件index.html,源码如下:
index.php:
query($sql); while($row_class=$db->fetch_array($query)){ $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]); } $smarty->assign("sm_class",$sm_class); $sql="select * from `p_newsbase` order by id DESC limit 5"; $query=$db->query($sql); while($row_news=$db->fetch_array($query)){ $sm_news[]=array('title'=>$row_news[title],'id'=>$row_news[id]); } $smarty->assign('sm_news',$sm_news); $sql="select * from `p_config`"; $query=$db->query($sql); while($row_config=$db->fetch_array($query)){ $sm_config[$row_config[name]]=$row_config[values]; } $smarty->assign('sm_config',$sm_config); $smarty->display("index.html"); ?>
index.html:
{$sm_config.websitename}
姓名:
电话:
OICQ:
手机:
地址:
入贯彻落实科学发展观的自觉性和坚定性湖南一 考生高考4门课程故意考零分温家宝调研太湖污染代表 中央向居民致歉湖南73人涉黑集团麻醉强奸女服务员 被公诉女大学生卖淫被抓警察让其参加毕被公诉女大学生卖淫被抓警察让其参加毕...[详细]
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 我的邮件:{$sm_config.website_email}
(7)新闻列表页
包含列表程序文件list.php,smarty模板文件:list.html
list.php
findall('p_config'); while($row_config=$db->fetch_array($query)){ $sm_config[$row_config[name]]=$row_config[values]; } $smarty->assign('sm_config',$sm_config); //配置导航 $sql="select * from `p_newsclass` where f_id=0 order by id DESC"; $query=$db->query($sql); while($row_class=$db->fetch_array($query)){ $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]); } $smarty->assign('sm_class',$sm_class); $query = $db->findall("p_newsclass"); while ($row = $db->fetch_array($query)) { $news_class_arr[$row[id]] = $row[name]; } //打印左侧当前文章栏目分类 $query=$db->findall("p_newsclass where f_id = $_GET[cid]"); while($row_chlidclass=$db->fetch_array($query)){ $news_class_in.=$row_chlidclass[id].","; $news_class_list_arr[]=array('name'=>$row_chlidclass[name],'id'=>$row_chlidclass[id]); } $news_class_in.="$_GET[cid]"; //文章列表 $sql="select id from `p_newsbase` where cid in ($news_class_in)"; $total=mysql_num_rows(mysql_query($sql)); pageft($total,2); if($firstcount<0) $firstcount=0; $query=$db->query("select * from `p_newsbase` where cid in ($news_class_in) limit $firstcount,$displaypg"); while($row_list=$db->fetch_array()){ $sm_list[]=array( "title"=>$row_list[title], 'cid'=>$row_list[cid], 'id'=>$row_list[id], 'cidname'=>$news_class_arr[$row_list[cid]]); } $smarty->assign("news_class_list_arr",$news_class_list_arr); $smarty->assign("sm_list",$sm_list); $smarty->assign("pagenav", $pagenav); //新闻分页 $smarty->display("list.html"); ?>
list.html
{$sm_config.websitename}
{section name=l loop=$news_class_list_arr}
{$news_class_list_arr[l].name}
{/section}
{$sm_list[l].date_time}
{/section} {$pagenav}
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 我的邮件:{$sm_config.website_email}
(8) 新闻内容页
包括新闻内容程序文件view.php,smarty模板文件view.html
view.php
query($sql); while($row_config=$db->fetch_array($query)){ $sm_config[$row_config[name]]=$row_config[values]; } $smarty->assign("sm_config",$sm_config); //配置栏目导航 $sql="select * from `p_newsclass` where f_id=0"; $query=$db->query($sql); while($row_class=$db->fetch_array($query)){ $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]); } $smarty->assign("sm_class",$sm_class); $sql="select * from `p_newsbase` as a,p_newscontent as b where a.id=b.nid and a.id=$_GET[id]"; $query=$db->query($sql); $row_news=mysql_fetch_array($query,MYSQL_ASSOC); $smarty->assign("row_news",$row_news); $smarty->display("view.html"); ?>
view.html
{$sm_config.websitename}
{$row_news.content}
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 目前已有[2222]点击
The above is the detailed content of Development of Micro News System. For more information, please follow other related articles on the PHP Chinese website!