Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否在每页上都进行了登录检查_Php_Codeigniter - Fatal编程技术网

Php 是否在每页上都进行了登录检查

Php 是否在每页上都进行了登录检查,php,codeigniter,Php,Codeigniter,我正在使用CodeIgniter。我有一个名为abc的控制器,我有名为index、a、b和c的函数 www.example.com/abc/ 我希望该用户只能访问他登录的区域 www.example.com/abc/ //if loggged in else back to homepage 或 现在检查登录。我使用: if($this->auth->is_logged_in()){..}else{redirect('/');} 在每个函数上单独设置 还

我正在使用CodeIgniter。我有一个名为
abc
的控制器,我有名为
index
a
b
c
的函数

www.example.com/abc/
我希望该用户只能访问他登录的区域

www.example.com/abc/                    //if loggged in else back to homepage

现在检查登录。我使用:

if($this->auth->is_logged_in()){..}else{redirect('/');}

在每个函数上单独设置


还有其他方法吗???

我认为可以通过重写构造函数并在其中调用函数来实现这一点

<?php
class Blog extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            // check login
       }
}
?>

对于特定控制器,您可以将if检查放入控制器的
构造函数中,以便在调用控制器的任何方法时,它都将通过if检查

class Abc extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            //your if goes here
       }
}
如果您想检查用户是否登录到整个应用程序中,您可以使用
CI\u Controller
的构造函数方法
\u construct()
,这样当用户访问应用程序中的任何控制器时,它都会被验证

class CI_Controller {

    private static $instance;

    /**
     * Constructor
     */
    public function __construct()
    {
           //your if goes here

    }

 }
class CI_Controller {

    private static $instance;

    /**
     * Constructor
     */
    public function __construct()
    {
           //your if goes here

    }

 }