程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php實戰第六天

php實戰第六天

編輯:關於PHP編程

今天學習了做後台頁面,很失敗就不展示了。

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];
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved