【问题标题】:php - Session are lost after redirect in CodeIgniter 3php - 在 CodeIgniter 3 中重定向后会话丢失
【发布时间】:2018-07-07 04:42:46
【问题描述】:

我想在 CodeIgniter 3 中获得一些帮助。每次我登录并重定向到索引页面时,会话都会丢失。

这是我的代码:

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Secretariat extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('SecretariatModel');
        $this->load->model('IndiRegModel');
        $this->load->model('RoomModel');
        $this->load->model('BuildingModel');
        $this->load->model('BilletModel');
        $this->load->model('BatchRegModel');
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    public function secretariatLogin(){

        if ($this->session->isSecretariat){
            redirect('secretariat/index', $data);


        } else{

            $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';

            $this->load->view('include/toppage', $data);
            $this->load->view('include/defaultnavbar', $data);
            $this->load->view('pacsa/slider');
            $this->load->view('secretariat/secretariatLogin', $data);
            $this->load->view('include/bottompage');

       }

    }

    public function signin(){
        $secretariat = array(
            'sec_email' => $this->input->post('sec_email'),
            'sec_password' => sha1($this->input->post('sec_password'))
        );

        $user = $this->SecretariatModel->getSecretariat($secretariat);
        //print_r($user->name);die();

        if(!$user == null){

            $newdata = array(
                'sec_id' => $user->sec_id,
                'sec_name'  => $user->sec_name,
                'sec_lastname' => $user->sec_lastname,
                'sec_email' => $user->sec_email,
                'sec_password' => $user->sec_password,
                'sec_status' => $user->sec_status,
                'sec_address' => $user->sec_address,
                'logged_in' => TRUE,
                'isSecretariat' => TRUE
            );

            $this->session->set_userdata($newdata);            
            redirect('secretariat/index');
        } else {
            $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
            $data['message'] = 'Invalid email or password';

            $this->load->view('include/toppage', $data);
            $this->load->view('include/defaultnavbar', $data);
            $this->load->view('pacsa/slider');
            $this->load->view('secretariat/secretariatLogin', $data);
            $this->load->view('include/bottompage');
        }
    }    

    public function index(){
        $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
        $id = $this->session->sec_id;
        var_dump($id);
        echo die();

        $this->load->view('include/toppage', $data);
        $this->load->view('include/secretariatnavbar', $data);
        $this->load->view('pacsa/slider');
        $this->load->view('secretariat/index', $data);
        $this->load->view('include/bottompage');
    }
}

所以在重定向到索引页面后,我想验证是否涉及会话。我试图回显用户的 ID 和名称,但我得到一个空值。

【问题讨论】:

  • 显示secretariat控制器的完整代码
  • 编辑了我的问题,谢谢
  • 登录成功后重定向的index函数secretariat/index在哪里?
  • 抱歉我的编辑不好再次感谢
  • 此类问题通常是由于会话和/或 cookie 配置不正确造成的。请显示那些$config 设置。

标签: php codeigniter session


【解决方案1】:

还要检查您的 php.ini 文件是否有此选项:

session.auto_start=1

【讨论】:

  • 在哪里可以找到 .ini 文件?
  • 您的操作系统或服务器平台是什么?当我使用 XAMPP 在 localhost 上运行我的网站时,通常会出现会话问题。 php.ini 位于 drive:/xampp/php/ 或者你可以在你的服务器上创建一个新的 .php 文件,并编写以下代码:&lt;?php phpinfo(); ?&gt; 并打开它来自您的浏览器,它将显示您的服务器的所有参数,包括您的 php 配置文件 (php.ini) 位置
  • 为我工作。我以为问题出在框架上!谢谢!
  • 为我工作!问题出在 Mac 上的 XAMPP 上。默认设置为 session.auto_start=0
  • 我几乎失去了几天和几天的时间来寻找这个简单而完美的 Codeigniter 会话问题的答案!谢谢索菲安!
【解决方案2】:

在此链接中:

https://php.developreference.com/article/23484402/Codeigniter+session+data+lost+after+redirect

如果您使用 CI 3.x 并且刚刚将服务器 php 版本升级到 php 7.x 转到第 281 行的 system/libraries/Session/session.php 并将 ini_set('session.name', $params['cookie_name']); 替换为 ini_set('session.id', $params['cookie_name']);

【讨论】:

  • 前面几行,还有$params['cookie_name'] = ini_get('session.id');
【解决方案3】:

前往

系统/库/Session/session.php

在第 281 行并替换

ini_set('session.name', $params['cookie_name']); 

通过

ini_set('session.id', $params['cookie_name']);

此问题在升级PHP更高版本到7.3+时正常出现

【讨论】:

  • 谢谢你帮我工作,你救了我的夜晚
  • 如果我们用 php 5.x 替换那个可以吗?
  • 是的,我认为PHP版本应该没有问题。不过,如果您发现任何困难,请在此处发布。
【解决方案4】:

您是否加载了会话库

this-&gt;load-&gt;library('session')

或者通过自动加载

$autoload['libraries'] = array('session');

【讨论】:

  • 是的,我已经将它加载到我的控制器构造函数中,是的,它也加载到了 autoload.php 中
  • @KyleCipriano 如果它已加载到您的 autoload.php 文件中,则无需将其加载到控制器中
  • Codeigniter 加载后会自动忽略它
  • 您是否正在使用其他与会话相关的库?
  • 没有。只是来自控制器或自动加载的会话本身
【解决方案5】:

我刚刚解决了我的问题原来我有旧版本的 codeigniter 3 我将我的 CI 升级到网站上提供的最新版本。谢谢大家的帮助

【讨论】:

    【解决方案6】:

    您的问题并非来自会话本身。

    尝试:

    • 评论重定向方法并添加var_dump($user),以查看您的$user 是否设置正确。问题可能来自您的 $user 对象,其中包含 idname 的空值。

    • if(!$user == null){ 更改为if ($user != null) {if ($user) {

    【讨论】:

    • 谢谢。好的,我尝试在 $user 上使用 var_dump 并且我从我尝试登录的用户那里获取了所有数据然后我做了你告诉我更改 if 语句但是当我使用索引上的 var 转储来检查我是否从会话中获取 id 和 name,我仍然得到 NULL 值
    • 在你的索引方法中,能告诉我var_dump($this-&gt;session-&gt;userdata())的结果吗?
    • hmmm 我得到了这个结果 array(1) { ["__ci_last_regenerate"]=> int(1517140453) }
    • 再次注释redirect('secretariat/index'); 行,并在set_userdata 之后添加它:var_dump($this-&gt;session-&gt;userdata());。这里的结果是什么?
    • 我得到了这个结果 array(10) { ["__ci_last_regenerate"]=> int(1517140803) ["sec_id"]=> string(1) "1" ["sec_name"]=> string (11) "Secretariat" ["sec_l​​astname"]=> 字符串(9) "asdfghjkl" ["sec_email"]=> 字符串(11) "secretariat" ["sec_password"]=> 字符串(40) "92a87dfd4ec6c963caeb9e47ea97dd6f4f9c8441" [" sec_status"]=> string(6) "单人" ["sec_address"]=> string(10) "asdfghjkl;" ["logged_in"]=> bool(true) ["isSecretariat"]=> bool(true) } 我从我尝试登录的用户那里获得了所有数据
    【解决方案7】:

    如果您使用的是 PHP 7.1+/7.2,就会出现这个问题。将 PHP 版本更改为 down(以检查它是否有效)。

    【讨论】:

    • 我已经将 CodeIgniter 3 与 PHP 7.2 一起使用,并且会话运行良好。你的回答不是很有帮助。建议不同版本的 PHP 并不能解决任何问题。
    • 我在本地主机和实时服务器中的一个应用程序中遇到了这个问题,然后我更改了 PHP 版本,现在它可以正常工作了。
    【解决方案8】:

    我们也遇到同样的问题。我们的规范:InvoicePlane,PHP 7.0,代码点火器 v.3.1.11。每个使用 redirect() 帮助器的控制器都从浏览器获得 err_connection_refused。

    经过数小时详尽的调试。我们解决了这个by change IP Address base_url to domain name and set certificate from letsencrypt.

    【讨论】:

      【解决方案9】:

      希望这会有所帮助,我正在使用 Homestead - Vagrant - php7.4,我让它工作的唯一方法是关注 this answer on Github

      添加到你的

      项目/应用程序/config/config.php

       /* 
       |-------------------------------------------------------------------------- 
       | Cookie Related Variables 
       |-------------------------------------------------------------------------- 
       | 
       | 'cookie_prefix'   = Set a cookie name prefix if you need to avoid collisions 
       | 'cookie_domain'   = Set to .your-domain.com for site-wide cookies 
       | 'cookie_path'     = Typically will be a forward slash 
       | 'cookie_secure'   = Cookie will only be set if a secure HTTPS connection exists. 
       | 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript) 
       | 
       | Note: These settings (with the exception of 'cookie_prefix' and 
       |       'cookie_httponly') will also affect sessions. 
       | 
       */ 
       $config['cookie_prefix']   = ''; 
       $config['cookie_domain']   = ''; 
       $config['cookie_path']     = '/'; 
       $config['cookie_secure']   = FALSE; 
       $config['cookie_httponly']     = FALSE; 
      

      还将你的行从 289 更改为 352

      项目/系统/核心/Input.php

      /** 
       * Set cookie 
       * 
       * Accepts an arbitrary number of parameters (up to 7) or an associative 
       * array in the first parameter containing all the values. 
       * 
       * @param   string|mixed[]  $name       Cookie name or an array containing parameters 
       * @param   string      $value      Cookie value 
       * @param   int     $expire     Cookie expiration time in seconds 
       * @param   string      $domain     Cookie domain (e.g.: '.yourdomain.com') 
       * @param   string      $path       Cookie path (default: '/') 
       * @param   string      $prefix     Cookie name prefix 
       * @param   bool        $secure     Whether to only transfer cookies via SSL 
       * @param   bool        $httponly   Whether to only makes the cookie accessible via HTTP (no javascript) 
       * @return  void 
       */ 
      public function set_cookie($name, $value = '', $expire = 0, $domain = '', $path = '/', $prefix = '', $secure = NULL, $httponly = NULL) 
      { 
          if (is_array($name)) 
          { 
              // always leave 'name' in last place, as the loop will break otherwise, due to $$item 
              foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item) 
              { 
                  if (isset($name[$item])) 
                  { 
                      $$item = $name[$item]; 
                  } 
              } 
          } 
        
          if ($prefix === '' && config_item('cookie_prefix') !== '') 
          { 
              $prefix = config_item('cookie_prefix'); 
          } 
        
          if ($domain == '' && config_item('cookie_domain') != '') 
          { 
              $domain = config_item('cookie_domain'); 
          } 
        
          if ($path === '/' && config_item('cookie_path') !== '/') 
          { 
              $path = config_item('cookie_path'); 
          } 
        
          $secure = ($secure === NULL && config_item('cookie_secure') !== NULL) 
              ? (bool) config_item('cookie_secure') 
              : (bool) $secure; 
        
          $httponly = ($httponly === NULL && config_item('cookie_httponly') !== NULL) 
              ? (bool) config_item('cookie_httponly') 
              : (bool) $httponly; 
        
          if ( ! is_numeric($expire) OR $expire < 0) 
          { 
              $expire = 1; 
          } 
          else 
          { 
              $expire = ($expire > 0) ? time() + $expire : 0; 
          } 
        
          setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly); 
      } 
      

      【讨论】:

        【解决方案10】:

        试试这个

        $this-&gt;output-&gt;profiler(true);

        查看会话是否真的设置并检查库是否已加载

        【讨论】:

        • 对不起,我对 CI 有点陌生,我会把这条线放在哪里?谢谢
        • 在你的控制器中
        • 它可以在你正在调用的函数或构造函数中
        猜你喜欢
        • 1970-01-01
        • 2012-08-17
        • 2021-01-29
        • 2013-06-19
        相关资源
        最近更新 更多