知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

php实战第六天

发布时间:2015-05-27 19:27:12作者:知识屋

今天学习了做后台页面,很失败就不展示了。

php方面:以下代码可以屏蔽php注意级别的错误,即 抛出任何非注意错误


[php]  error_reporting(E_ALL&~E_NOTICE); 

error_reporting(E_ALL&~E_NOTICE);
开启session的方法,开启后就可以使用$_SESION咯。

session是保存在服务端的东西噢。启动后会给 浏览器发送一个 session的ID,是cookies方式的

[php]  session_start(); 

session_start();
如果浏览器禁用了cookies那也不要紧,可以把session写到URL里就好咯.嘿嘿

[php]  session_name.'='.session_id() 

session_name.'='.session_id() 附上我写好的 adminAction,只有登陆功能哦


[php] <?php 
    /**
     * 
     **/ 
    class adminAction extends Action 
    { 
        protected $db=''; 
        function __construct() 
        { 
            // code...  
            // //$smarty->force_compile = true;  
            /*$this->db = new MySQL("127.0.0.1","root","1234","myly","data");
            $this->db->table('data');*/ 
            $this->db = MySQL::getClass(); 
            //var_dump($this->db);  
            $this->db -> connect("127.0.0.1","root","1234","myly"); 
 
            $this->db -> table('user'); 
 
            parent::__construct(); 
        } 
 
        public function index() 
        { 
            if($this->is_login()){ 
                echo "<script>window.location.href='admin.php?m=admin&a=admin';</script>"; 
            }else{ 
                echo "<script>window.location.href='admin.php?m=admin&a=login';</script>"; 
            } 
        } 
 
        private function is_login() 
        { 
            return $_SESSION['login']; 
 
        } 
 
        public function login() 
        { 
            if (!empty($_POST['userName']) && !empty($_POST['password'])) { 
                $userName=$_POST['userName']; 
                $password=md5($_POST['password']); 
                /*var_dumP($_POST);
                var_dump($password);
                var_dump($password);*/ 
                if($_SESSION['userData']=$this->db->where("userName='{$userName}' and password='{$password}'")->fine()){ 
                    echo "<script>window.location.href='admin.php?m=admin&a=admin';</script>"; 
                    $_SESSION['login']=true; 
                     
                }else{ 
 
                    echo "登录失败"; 
                } 
            } 
 
            $this->display(); 
        } 
 
        public function ulogin() 
        { 
            echo "<script>window.location.href='admin.php?m=admin&a=login';</script>"; 
            unset($_SESSION); 
        } 
 
        public function admin() 
        { 
            if(!$this->is_login()){ 
                echo "<script>window.location.href='admin.php?m=admin&a=login';</script>"; 
            } 
            //var_dump($_SESSION);  
            $this->assign('userName',$_SESSION['userData']['userName']); 
            $this->display(); 
        } 
 
        function add(){ 
            /*var_dump($this->db
                ->data(
                    array('userName' => 'admin',
                          'password' => md5('admin'),
                          'time'     => time()
                ))
                ->add());
                */ 
        } 
         
    } 
?> 

<?php
 /**
  *
  **/
 class adminAction extends Action
 {
  protected $db='';
  function __construct()
  {
   // code...
   // //$smarty->force_compile = true;
   /*$this->db = new MySQL("127.0.0.1","root","1234","myly","data");
   $this->db->table('data');*/
   $this->db = MySQL::getClass();
   //var_dump($this->db);
   $this->db -> connect("127.0.0.1","root","1234","myly");

   $this->db -> table('user');

   parent::__construct();
  }

  public function index()
  {
   if($this->is_login()){
    echo "<script>window.location.href='admin.php?m=admin&a=admin';</script>";
   }else{
    echo "<script>window.location.href='admin.php?m=admin&a=login';</script>";
   }
  }

  private function is_login()
  {
   return $_SESSION['login'];

  }

  public function login()
  {
   if (!empty($_POST['userName']) && !empty($_POST['password'])) {
    $userName=$_POST['userName'];
    $password=md5($_POST['password']);
    /*var_dumP($_POST);
    var_dump($password);
    var_dump($password);*/
    if($_SESSION['userData']=$this->db->where("userName='{$userName}' and password='{$password}'")->fine()){
     echo "<script>window.location.href='admin.php?m=admin&a=admin';</script>";
     $_SESSION['login']=true;
     
    }else{

     echo "登录失败";
    }
   }

   $this->display();
  }

  public function ulogin()
  {
   echo "<script>window.location.href='admin.php?m=admin&a=login';</script>";
   unset($_SESSION);
  }

  public function admin()
  {
   if(!$this->is_login()){
    echo "<script>window.location.href='admin.php?m=admin&a=login';</script>";
   }
   //var_dump($_SESSION);
   $this->assign('userName',$_SESSION['userData']['userName']);
   $this->display();
  }

  function add(){
   /*var_dump($this->db
    ->data(
     array('userName' => 'admin',
        'password' => md5('admin'),
        'time'     => time()
    ))
    ->add());
    */
  }
  
 }
?>
提交表单用的 login.html
[html] <html> 
<head> 
    <title>瀑布流留言板 后台登陆</title> 
</head> 
<body> 
    <form action="admin.php?m=admin&a=login" method="post"> 
        <input type="text" name="userName" value="admin"> 
        <input type="password" name="password" value="admin"> 
        <input type="submit" value="登陆"> 
    </form> 
</body> 
</html> 

<html>
<head>
 <title>瀑布流留言板 后台登陆</title>
</head>
<body>
 <form action="admin.php?m=admin&a=login" method="post">
  <input type="text" name="userName" value="admin">
  <input type="password" name="password" value="admin">
  <input type="submit" value="登陆">
 </form>
</body>
</html>登陆成功后用的 admin.html


[html]  <html> 
<head> 
    <title>欢迎 <{$userName}></title> 
</head> 
<body> 
 
</body> 
</html> 

<html>
<head>
 <title>欢迎 <{$userName}></title>
</head>
<body>

</body>
</html>
这个就是我的登陆功能了。

 


修改了 mysql.class.php 加了一个函数 fine,这样返回的数组就是 一维的咯,方便些


[php] /**
 +----------------------------------------------------------
 * 获取数据表的单条记录,返回一维数组
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 * @param 
 +----------------------------------------------------------
 */ 
public function fine(){ 
    $select_sql = 'select '; 
    $fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*'; 
    $select_sql.=$fields; 
    $select_sql.= ' from `'.$this->query_list['table'].'` '; 
     
    isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):''; 
    isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):''; 
    isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):''; 
    isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):''; 
    isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):''; 
    isset($this->query_list['limit'])?($select_sql.=' 1,1'):''; 
    //echo '<br>----->'.$select_sql.'<---------<br>';  
    return $this->query($select_sql)[0]; 

    /**
     +----------------------------------------------------------
     * 获取数据表的单条记录,返回一维数组
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param
     +----------------------------------------------------------
     */
    public function fine(){
        $select_sql = 'select ';
        $fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*';
        $select_sql.=$fields;
        $select_sql.= ' from `'.$this->query_list['table'].'` ';
       
        isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';
        isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):'';
        isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):'';
        isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):'';
        isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):'';
        isset($this->query_list['limit'])?($select_sql.=' 1,1'):'';
        //echo '<br>----->'.$select_sql.'<---------<br>';
        return $this->query($select_sql)[0];
    }

 

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜