Home  >  Article  >  Backend Development  >  Development of Micro News System

Development of Micro News System

PHPz
PHPzOriginal
2017-04-04 14:53:152185browse

1. System Description

"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)

2. Key points of system development

1.Smarty application
2.Backend membership, permission verification
3.Object, class encapsulation

3. System module classification

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:

Development of Micro News System

##Data table

2. Program FileWebsite
Directory structureList, as shown in the figure:

Development of Micro News System
##Website directory structure

IV. Detailed development explanation

The details will be explained item by item according to a certain process (

css and other files are not listed in detail

):1.mysql
Creation of databaseThe configuration diagrams of each table are pasted in turn:

    p_config table

Development of Micro News System
p_config table

    p_admin table

Development of Micro News System##p_admin table

p_newsclass table

##p_newsclass tableDevelopment of Micro News System

p_newsbase Table

##p_newsbase table

Development of Micro News System

p_newscontent table

p_newscontent table

Development of Micro News SystemDetailed explanation:

##p_newsclass, p_newsbase, p_newscontent

These three There is a relationship between the tables:

    p_newsbase|cid --> p_newsclass|id
  • p_newscontent|nid --> p_newsbase|id

    p_newsclass is a top-level column when

    f_id=0
  • ,
  • f_id=1

    means that the category belongs to a subcategory of the category with id 1. 2.smarty configuration

    Place the smarty file in the common folder of the root directory. Next step
  • Installation
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);     //模板路径变量映射

?>

Up to now, smarty has been configured. 3.mysql statement class

Here is a detailed description of the mysql custom statement class commonly used in the system: mysql.class.php, the source code is as follows:

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 "
";     }else{       echo "
";       echo "错误信息提示:
";       echo "

";       echo "

";       echo "错误号:12142";       echo "


";       echo "错误原因:" . mysql_error() . "

";       echo "

";       echo "" . $message . "";       echo "

";       echo "
" . $sql . "
";           $ip=$this->getip();           if ($this->bulletin) {         $time = date("Y-m-d H:i:s");         $message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";         $server_date = date("Y-m-d");         $filename = $server_date . ".txt";         $file_path = "error/" . $filename;         $error_content = $message;         //$error_content="错误的数据库,不可以链接";         $file = "error"; //设置文件保存目录         //建立文件夹         if (!file_exists($file)) {           if (!mkdir($file, 0777)) {             //默认的 mode 是 0777,意味着最大可能的访问权             die("upload files directory does not exist and creation failed");            }         }             //建立txt日期文件         if (!file_exists($file_path)) {           //建立 “建立日期文件”           fopen($file_path,"w+");           //首先要确定文件存在并且可写           if (is_writable($file_path)) {             //使用添加模式打开$filename,文件指针将会在文件的开头             if(!$handle=fopen($file_path,'a')){               echo "不能打开文件 $filename";               exit;             }             //将$somecontent写入到我们打开的文件中。             if(!fwrite($handle,$error_content)){               echo "不能写入到文件 $filename";               exit;             }             echo "——错误记录被保存!";             //关闭文件             fclose($handle);           }else{             echo "文件 $filename 不可写";           }         }else{           //首先要确定文件存在并且可写           if (is_writable($file_path)) {             //使用添加模式打开$filename,文件指针将会在文件的开头             if (!$handle = fopen($file_path, 'a')) {               echo "不能打开文件 $filename";               exit;             }             //将$somecontent写入到我们打开的文件中。             if (!fwrite($handle, $error_content)) {               echo "不能写入到文件 $filename";               exit;             }             //echo "文件 $filename 写入成功";             echo "——错误记录被保存!";             //关闭文件             fclose($handle);           }else{             echo "文件 $filename 不可写";           }         }       }       echo "
";       if($this->is_error){         exit;       }     }     echo "

";     echo "
";     echo "
";   }     /*获得客户端真实的IP地址*/     function getip() {           if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {             $ip = getenv("HTTP_CLIENT_IP");           } else           if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {                 $ip = getenv("HTTP_X_FORWARDED_FOR");             } else                 if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {                     $ip = getenv("REMOTE_ADDR");                 } else                     if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {                         $ip = $_SERVER['REMOTE_ADDR'];                     } else {                         $ip = "unknown";                     }         return ($ip);         }       } ?>

4. Backend login and user permission judgmentBackground login and user permission judgment are mainly implemented through
session

encryption, encapsulating the permission judgment function, and in each background page

Quote is enough. (1). First, we need to create the background call file admin_global.php. The source code is as follows:

(2). Create the action.class.php file, including User login, permission judgment and other functions, 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 . '
                      2秒后返回指定页面!
                      如果浏览器无法跳转,请点击此处

                

                                 ';         echo $msg;         exit ();     }   } ?>

(3). Log in to the homepage/admin/index.php, the source code is as follows:
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
    
    
    

 
    
                                                                       

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","更新成功");    
}
?>



后台管理



          
                                                     
添加分类
                 

                       $val){ ?>                      
系统分类
               findall("p_newsclass where f_id=$id");     while($row_fid=$db->fetch_array($query_fid)){ ?>   
   ┗            
  
    

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","删除成功");    
}

?>




后台管理



          
                            findall("p_newsbase limit  $firstcount, $displaypg");    while ($row = $db->fetch_array($query)) {    ?>                                                                       
新闻分类新闻标题作者日期操作
'>删除 / '>修改

    

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)]

       现在已经有[122]次点击   

    

          

            

促销活动

MORE
            

            
                {section name=l loop=$sm_news}                 
  • {$sm_news[l].title}
  •             {/section}               

              

                

    公司简介

    MORE

                

                入贯彻落实科学发展观的自觉性和坚定性湖南一 考生高考4门课程故意考零分温家宝调研太湖污染代表 中央向居民致歉湖南73人涉黑集团麻醉强奸女服务员 被公诉女大学生卖淫被抓警察让其参加毕被公诉女大学生卖淫被抓警察让其参加毕...[详细]           

              


              

                

    产品展示

    产品分类:展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示

    MORE

                

                
                      
    •                 

      产品展示

                      
                            
      • 规格:
      •                     
      • 产地:
      •                     
      • 价格:1200 [详细]
      •                 
                      
    •                 
    •                 

      产品展示

                      
                            
      • 规格:
      •                     
      • 产地:
      •                     
      • 价格:1200 [详细]
      •                 
                      
    •                 
    •                 

      产品展示

                      
                            
      • 规格:
      •                     
      • 产地:
      •                     
      • 价格:1200 [详细]
      •                 
                      
    •                 
    •                 

      产品展示

                      
                            
      • 规格:
      •                     
      • 产地:
      •                     
      • 价格:1200 [详细]
      •                 
                      
    •             

      

      

         

              网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们           我的邮件:{$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}           

  

    


    

      

类别

      新闻标题       

时间

    

    {section name=l loop=$sm_list}      

      

{$sm_list[l].cidname}

      {$sm_list[l].title}       

{$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.title}
       
     

     时间:{$row_news.date_time|date_format:"%D"} 作者:{$row_news.author}
     
     
     
     

     {$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn