zoukankan      html  css  js  c++  java
  • php 用户访问菜单页面,必须登录,判断用户是否登录

    <pre name="code" class="python">#
    
    
    本节课大纲:
    一、空模块和空操作
    	1、空操作
    		function _empty($name){
    			$this->show("$name 不存在 <a href='__APP__/Index/index'>返回首页</a>");
    		}
    	2.空模块
    	class EmptyAction extends Action{
    		function index(){
    			$city=M('City');
    			$arr=$city->select();
    			$this->assign('list',$arr);
    			$name=MODULE_NAME;
    			$this->display("City:$name");
    		}
    	}
    	
    二、前置操作和后置操作
    	1、前置操作: _before_操作名
    	2、后置操作: _after_操作名
    
    
    
    方法代码重用:
    
    
    类:
    <?php
    // 本类由系统自动生成,仅供测试用途
    class IndexAction extends Action {
        public function index(){
    	$city=M('city'); //返回Model实例
    	//返回数组
    	$arr=$city->select();
    	//dump($arr);
    	$this->assign('list',$arr);
    	$this->display();
        }
    	
    	public function next(){
    		$this->display();
    	}
    }
    
    
    调用Index 控制器的index方法:
    
    
    
    <?php
      class CityAction extends Action{
    	  function bj(){
    // new一个对象
    		  $oi=new IndexAction();
    // 调用对象的方法
    		  $oi->index();
    	  }
    	  
    	  function sh(){
    	  $oi=new IndexAction();
    		  $oi->index();
    	  }
    	  
    	  function gz(){
    	  $oi=new IndexAction();
    		  $oi->index();
    	  }
    	  
    	   function _empty($name){
    			$this->show("$name 不存在 <a href='__APP__/Index/index'>返回首页</a>");
    		}
      }
    
    ?>
    
    
    
       
     <!-- Login模块下的do_login处理 -->
     <form action="__URL__/do_login"  method='post'>
    
    解析成:
    
    
    <!-- Login模块下的do_login处理 -->
        <form action="/thinkphp3/index.php/Login/do_login"  method='post'>
    
    
    
    
    ///用户访问菜单页面,必须登录,判断用户是否登录
    <?php
       class LoginAction extends Action{
    	   function index(){
    		   $this->display();
    	   
       }
      public function do_login() {
    	  $username=$_POST['username'];
    	  $password=$_POST['password'];
    	  $user=M('user');
    	  $where['username']=$username;
    	  $where['passord']=$password;
    	  $c=$user->where($where)->count();
    	  
    	  if ($c>0){
    		  //向SESSION里写数据
    		  $_SESSION['username']=$username;
    		  $this->redirect('Index/index');
    	  }else{
           $this->error('用户不能登录');
    	  }
    	  
      }
       }
    ?>
    
    
    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>Document</title>
     </head>
     <body>
       
       <!-- Login模块下的do_login处理 -->
        <form action="__URL__/do_login"  method='post'>
    	  用户名:<input type='text' name='username'/><br/>
    
    	  密码:<input type='password' name='password'/><br/>
    
    	  <input type='submit' value='登录'/>
          </form>
     </body>
    </html>
    
    
    <?php
    // 本类由系统自动生成,仅供测试用途
    class IndexAction extends Action {
    	public function _before_index(){
    		//做判断,如果没有登录,跳转到登录页面
    		if(!isset($_SESSION['username']) || $_SESSION['username']=='')
    		{
    			$this->redirect('Login/index');
    		}
    		
    		
    		
    	}
        public function index(){
    	$city=M('city'); //返回Model实例
    	//返回数组
    	$arr=$city->select();
    	//dump($arr);
    	$this->assign('list',$arr);
    	$this->display();
        }
    	
    	public function next(){
    		$this->display();
    	}
    }
    


    
                                        
    
  • 相关阅读:
    Java Web开发——JSP基本语法杂记
    Java Web开发——HTML CSS JavaScript 杂记
    Leetcode#13 Roman to Integer
    Leetcode#20 Valid Parentheses
    Leetcode#88 Merge Sorted Array
    Leetcode#171 Excel Sheet Column Number
    Leetcode#168 Excel Sheet Column Title
    Leetcode#160 Intersection of Two Linked Lists
    Leetcode#6 ZigZag Conversion
    Leetcode#8 String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350837.html
Copyright © 2011-2022 走看看