douou1891 2012-12-11 12:55
浏览 55

PHP:cookie存在,但函数没有使用isset找到它

Using CodeIgniter I'm checking if a cookie exists by using the isset() function and depending on that I redirect to an other controller. Now my problem is that I get a redirect-loop because the check_logged_in function thinks that the cookie doesn't exists.

Here is my code:

This function resides in the login_helper.

function check_logged_in()  
{  
    if(!isset($_COOKIE['uid']))  
    {   
        redirect('users/login');  
        return FALSE;   
    }
    else
    { 
         return TRUE;
}

this next function resides in the home controller and calls the check_logged_in() function, described above.

public function index()
{
    $this->load->helper('url');
    $this->load->helper('login');
    $logged_in = check_logged_in();

    if($logged_in === TRUE)
    {
        $this->load->view('templates/header');
        $this->load->view('pages/home_view');
        $this->load->view('pages/advanced_search_view');
        $this->load->view('pages/contact_information_view');
        $this->load->view('templates/footer');
    }
}

the next function resides in the users controller

public function login()
{
    if(isset($_COOKIE['uid']))
    { 
        redirect(home); 
    }
    else
    {
        -some unrelated code-
    }

when the cookie does exist and I run either the home/index function or the users/login function I end up in a redirect loop. When the cookie doesn't exist I end up at my login page, so it seems the users/login cookie check works correctly but the home/index function does not.


I have tried putting

if(!isset($_COOKIE['uid']))  
{   
    redirect('users/login');     
}

directly in the home/index function but it gave the same problems.

  • 写回答

1条回答 默认 最新

  • duanheyi7147 2012-12-11 19:15
    关注

    Put the login check in the Constructor. Give it a generic name - and have it do something useful like return info from the cookie, and/or from a database lookup.

    function __construct() {
    parent::__construct();
     // load the model
     $this->load->model( 'login_model' );
    
     // if logged in, return $this->user
     if(! $this->user = $this->login_model->verifyUser() ) 
     { redirect('users/login', 'refresh'); }
    
     } // construct
    

    And now $this->user is available for your methods, all the way to the view.

    If you need an "admin" that can do more then a user, in the same way

     $this->admin = $this->login_model->verifyAdmin() ;
    

    Then you can change what happens in verifyAdmin(), and your class does not have to get updated.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序