简体   繁体   English

PHP session 变量未找到未定义索引:is_user_login

[英]PHP session variables not found Undefined index: is_user_login

The session variables are not set.未设置 session 变量。 My website was working until last month.我的网站一直工作到上个月。 We haven't made any code change and all of a sudden login is not working and it is because the session variables are not being set.我们没有进行任何代码更改,突然登录不起作用,这是因为没有设置 session 变量。 As you can see in the code below, on querying the db for the emailaddress, the user is redirected to dashboard but there the variable is_user_login is not found.正如您在下面的代码中看到的,在查询数据库的电子邮件地址时,用户被重定向到仪表板,但没有找到变量 is_user_login。 Could there be any configuration change that occured during the month perhaps that could cause this.本月是否有任何配置更改可能会导致这种情况。

       if ($val->result_id->num_rows == 1) {
            foreach ($val->result_array() as $recs => $res) {
                $this->session->set_userdata(array(
                    'id' => $res['id'],
                    'email' => $res['email'],
                    'is_user_login' => true
                    )
                );
                $_SESSION['is_user_login'] = true;
            }
            redirect('dashboard');
          }

在此处输入图像描述

class Dashboard extends MY_Controller {
public function __construct() {
   parent::__construct();
     $f = basename(__FILE__);
    error_log("Inside file before: $f and is user currently logged in: {$this->session->userdata('is_user_login')}");
   if (!$this->session->userdata('is_user_login')) {
        redirect('login');
    }
}

When I print the session variable in the next page, it is empty.当我在下一页打印 session 变量时,它是空的。

UPDATE: I had to upgrade codeigniter from 3.0.6 to latest version.更新:我必须将 codeigniter 从 3.0.6 升级到最新版本。 This solved the issue.这解决了这个问题。

This php configuration is OK这个php配置OK

if this is really true that the session is not working then you must probably start the sessions.如果这确实是 session 不工作,那么您可能必须启动会话。

I would now include a little code to see what the situation is:我现在将包含一些代码来查看情况:


// include this code somewhere in your site into the same class 
protected function check_session($f,$l){
  if (session_status() == PHP_SESSION_NONE) {
    error_log("Sessions are enabled, but no session has been started in file:$f line:$l");
  }else if(session_status() == PHP_SESSION_DISABLED) {
    error_log("Sessions are currently disabled in file:$f line:$l");
  }else if(session_status() == PHP_SESSION_ACTIVE) {
    error_log("Sessions are enabled and a session has been started in file:$f line:$l");
  }
}


if ($val->result_id->num_rows == 1) {
  
  // then check status of sessions
  $this->check_session(basename(__FILE__),__LINE__);
  
  foreach ($val->result_array() as $recs => $res) {
    $this->session->set_userdata(array(
      'id' => $res['id'],
      'email' => $res['email'],
      'is_user_login' => true
      )
    );
    $_SESSION['is_user_login'] = true;
  }
  redirect('dashboard');
}


if your sessions are enabled but not started then you need to start them.如果您的会话已启用但未启动,那么您需要启动它们。

if the sessions are not enabled then you need to enable them.如果会话未启用,那么您需要启用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM