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();
	}
}


 

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6200128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值