Skip to content

Instantly share code, notes, and snippets.

@edk24
Created May 14, 2019 05:50
Show Gist options
  • Save edk24/fa922b985a99ac6cc5c1db06e59b4987 to your computer and use it in GitHub Desktop.
Save edk24/fa922b985a99ac6cc5c1db06e59b4987 to your computer and use it in GitHub Desktop.
CodeIgniter 控制器, 检测用户是否登录
<?php
/**
* 初始控制器
*/
class MY_Controller extends CI_Controller
{
// 是否检查用户登录状态
public $check_login = true; // 默认检查用户是否登录, 子类可以在 __construct() 中禁用它
public function __construct()
{
parent::__construct();
// 加载辅助函数
$this->load->helper('url');
$this->load->helper('redirect'); // 加载重定向跳转提示, 它在我的另一个代码片中
// 初始化session
$this->load->library('session');
// 检查登录状态
if ($this->check_login==true && $this->session->has_userdata('user')==false) {
jump_error('请登录后再进行操作!', 'Login/index'); // 友好的重定向跳转到登录页面
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment