Home > Backend Development > PHP Tutorial > The sixth day of PHP practice_PHP tutorial

The sixth day of PHP practice_PHP tutorial

WBOY
Release: 2016-07-14 10:10:54
Original
874 people have browsed it

Today I learned how to make a backend page. If it fails, it won’t be displayed.

PHP: The following code can shield PHP attention level errors, that is, throw any non-attention errors


[php] error_reporting(E_ALL&~E_NOTICE);

error_reporting(E_ALL&~E_NOTICE);
How to open session, you can use $_SESION after opening it.

Session is something saved on the server side. After startup, a session ID will be sent to the browser in the form of cookies

[php] session_start();

session_start();
It doesn’t matter if the browser disables cookies, you can just write the session into the URL. Hehe

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

session_name.'='.session_id() Attached is the adminAction I wrote, which only has the login function


[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());
                */ 
        } 
         
    } 
?> 

 /**
  *
  **/
 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]  
 
    瀑布流留言板 后台登陆 
 
 
   

 
         
         
         
   
 
 
 



 瀑布流留言板 后台登陆


 


  
  
  
 


登陆成功后用的 admin.html


[html]   
 
    欢迎 <{$userName}> 
 
 
 
 
 



 欢迎 <{$userName}>



这个就是我的登陆功能了。

 


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


[php] /**
+------------------------------------------------- ----------
* Get a single record of the data table and return a one-dimensional array
+------------------------------------------------- ----------
* @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 '
----->'.$select_sql.'<---------
';  
    return $this->query($select_sql)[0]; 

    /**
+------------------------------------------------- ----------
* Get a single record of the data table and return a one-dimensional array
+------------------------------------------------- ----------
* @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 '
----->'.$select_sql.'<---------
';
        return $this->query($select_sql)[0];
    }

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477413.htmlTechArticleToday I learned how to make a backend page. If it fails, it will not be displayed. PHP: The following code can shield PHP attention level errors, that is, throw any non-attention errors [php] error_reporting(E_...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template