【问题标题】:Session check Not working Properly - CodeIgniter会话检查无法正常工作 - CodeIgniter
【发布时间】:2015-02-27 12:59:36
【问题描述】:

我已将login控制器设置为默认控制器

在我的模型 Membership 中,我有一个函数 checkLogin(),如果会话有有效数据,则返回 true

checkLogin() 方法运行良好。

我在构造函数中调用checkLogin()方法如下。

方法 checkLogin() 工作正常,但不知道为什么当我加载 Login/postSignin 时该方法没有重定向到应用程序。

P.S:刷新页面后,它会自动重定向。因此,我确信逻辑中存在一些错误。

任何帮助将不胜感激

<?php
#File : ../controllers/Login.php

class Login extends CI_Controller{

    #Global Variables and Models to be loaded are initiated here
    public function __construct(){
        parent::__construct();
        /*
        |
        |   Load the Following Models
        |   1.Membership
        |
        */
        $this->load->model('Membership');
        #Check for Login details in the cookie
        #If checkLogin() returns true, it indicates that the user has logged in already
        if($this->Membership->checkLogin())
        {
            #redirect to ../controllers/Application Controller
            Redirect('Application');
        }
    }
    #Loads the Basic Header | Footer | Sidebar  && a Specific view->Signin
    public function index(){
        $this->load->view('include/Header');
        $this->load->view('include/SideBar');
        $this->load->view('Signin');
        $this->load->view('include/Footer');
    }
    /*
    |
    |
    |
    */
    public function  postSignin(){
        #Setting custom delimiters
        $this->form_validation->set_error_delimiters('<div class="form-group col-sm-8 center-block form-error bg-danger text-danger text-center">', '</div>');
        /*
        |
        |   ** Rules **
        |   Email       ->  required, Valid email
        |   Password    ->  required, minimum length=5, maximum length=12
        */      
        $this->form_validation->set_rules('emailid','Email','required|valid_email');
        $this->form_validation->set_rules('password','password','required|min_length[5]|max_length[12]|');
        #if the validation failes
        if ($this->form_validation->run() == FALSE)
        {
            #load the Header | Footer | Sidebar | Signin views again
            $this->load->view('include/Header');
            $this->load->view('include/SideBar');
            $this->load->view('Signin');
            $this->load->view('include/Footer');
        }
        else
        {
            #If checkCredentials() method returns true, it indicates a valid login
            if($this->Membership->checkCredentials())
            {   
                $emailid=$this->input->post('emailid');

                #Create an array to store all the necessary details
                $session_data=array(
                        'user_id'=>$this->Membership->getUserId($emailid),
                        'email'=>$emailid,
                        'first_name'=>$this->Membership->getFirstName($emailid),
                        'last_name'=>$this->Membership->getLastName($emailid),
                    );
                #Set the session data using array formed above
                $this->session->set_userdata($session_data);
                #Redirect to the next controller '../controllers/Application'
                Redirect('Application');        
            }
            #If checkCredentials() returns false, load views and display appropriate message
            else
            {
                #set login-status to 0
                $data['login_status']=0;
                #load the Header | Footer | Sidebar | Signin views again
                $this->load->view('include/Header');
                $this->load->view('include/SideBar');
                $this->load->view('Signin',$data);
                $this->load->view('include/Footer');
            }
        }       
    }
}
?>

【问题讨论】:

  • 为什么登录成功后无法加载视图?比如 $this->load->view('view_name_here');
  • 因为我需要另一个控制器来接管主应用程序。如果可能,您是否知道从一个控制器传递到下一个控制器的特定方式?
  • redirect 实际上应该可以工作..不太确定,但您能否将 Redirect 更改为重定向并使 R 变小而不是大写?
  • 不!它没有用!我的意思本质上是当我从Login/postSignin 导航到Application 后按下back 按钮时,它实际上会回到Login/postSignin。但是当我按下刷新它成功回到Application
  • 应用程序正在扩展 CI_ 或其他一些基本控制器?此外,好的 popint 将创建本身具有检查部分的核心控制器,然后使用:class Application extends MY_Login_check {}

标签: php codeigniter session


【解决方案1】:

您可以制作将处理该部分代码的核心控制器,检查登录状态。您可以使用任何需要登录检查的关闭应用程序控制器来扩展该控制器。

<?php if ( ! defined('BASEPATH')) exit('No direct access allowed');
//this is in APPPATH . 'core/'
class MY_Logincheck extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    /*
    |
    |   Load the Following Models
    |   1.Membership
    |
    */
    $this->load->model('Membership');
    #Check for Login details in the cookie
    #If checkLogin() returns true, it indicates that the user has logged in already
    if( ! $this->Membership->checkLogin()) {
        #redirect to ../controllers/Application Controller
        redirect('login', 'refresh');
        exit();// check switched to be firstly seen if not login passed
    }
  }
}

<?php if ( ! defined('BASEPATH')) exit('No direct access allowed');

class Application extends MY_Logincheck
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    // some code related to application
  }
}

您需要考虑的事项很少。 1.) 我不确定您是否会在没有适当的 .htaccess 文件代码的情况下获得到应用程序控制器的良好路由。 2.) 检查一些库,如 Ben Edmund 的 Ion_Auth for CI,它可以让事情变得更容易。 3.) 仔细检查是否加载了所有需要的帮助程序和库。 4.) 没有模型代码,我不能说更多。

【讨论】:

  • 谢谢 :) 我一定会检查一下 :),但我也希望 Application 扩展 CI_Controller 吗?是否可以从两个类扩展Application
  • class Some_class extends CI_Controller{} class Application extends Some_class{} 你已经拥有了来自 CI_Controller 的所有继承。检查 PHP 文档。 ClickTwoThree.
【解决方案2】:

Cache 是这里的罪魁祸首!我用这些把它关掉了。现在它不回去了。谢谢大家的帮助。

$this->output->set_header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
$this->output->set_header("Pragma: no-cache");

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多