【问题标题】:Codeigniter 3.0.3 sessions are not persistentCodeigniter 3.0.3 会话不是持久的
【发布时间】:2016-01-02 06:54:21
【问题描述】:

我遇到了一个非常奇怪的问题,Codeigniter 3.0.3 中的会话没有保存用于下一个请求。每次请求完成时,它都会创建一个新的会话记录,并且下次不会使用它。

奇怪的是,它确实可以在 HTTPS 版本的网站上工作,但不能在 HTTP 上工作。

场景: 在我网站的登录页面上,我进行了 AJAX 调用(在此调用中,我设置了一些会话变量和 flashdata)。收到成功消息后,我将页面重新加载到个人资料页面。

当我使用 HTTP 时,整个过程都不​​起作用,而是使用 HTTPS。

任何帮助将不胜感激。

EDIT v1: 此外,即使是 CSRF 也无法通过 HTTP 工作。我禁用它来测试系统。

EDIT v2:@DFriend 请求的代码

config.php

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'jupiter';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = "hkr_sessions"; 
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

$config['cookie_prefix']    = '';
$config['cookie_domain']    = '.jupiter.rjv.me';
$config['cookie_path']      = '/';
$config['cookie_secure']    = TRUE; // PS: tried FALSE version as well, but no change. 
$config['cookie_httponly'] = FALSE;

User.php 控制器

public function login_required() {
  if (!$this->logged_in()) {
    $this->session->set_flashdata("login_error", "You have to be logged in to see this page.");
    $this->session->set_userdata('redirect_back', $this->agent->referrer());
    redirectt('/login');
  }
}

routes.php

$route['login/required'] = 'user/login_required';

像这样的简单方法,当我浏览到http://domain.ltd/login/required 时,它会重定向到http://domain.ltd/login,并打印类似“您必须登录才能看到此页面。”的消息。。一旦用户登录,它会将用户重定向回他/她以前的页面。

在我的情况下,它会重定向到/login 页面,但不会打印出 flashdata 消息。

这里是 MY_Controller.php

class MY_Controller extends CI_Controller {
  protected $logged_in = NULL;
  protected $is_ajax   = NULL;
  protected $user_id   = NULL;

  public function __construct() {
    parent::__construct();

    log_message("DEBUG", "session variables: " . print_r($this->session->all_userdata(), true));

    $this->logged_in = $this->session->userdata('logged_in');
    $this->is_ajax = $this->input->is_ajax_request();
    $this->user_id = $this->session->userdata('user_id');
  }
}

如您所见,我在每个请求上打印出会话变量。

这是通过 HTTP 请求的会话的输出:

DEBUG - 2016-01-03 07:17:27 --> session variables: Array
(
    [__ci_last_regenerate] => 1451805447
)

这是通过 HTTPS 请求的会话的输出:

DEBUG - 2016-01-03 07:19:44 --> session variables: Array
(
    [__ci_last_regenerate] => 1451805564
    [redirect_back] => https://jupiter.rjv.me/book/1497-sefiller-viktor-mari-huqo
)

我没有更改任何代码,只是尝试了 HTTP 和 HTTPS 这两个请求。会话输出不同。一个不保存,另一个保存。我希望这能帮助您找出问题所在。

【问题讨论】:

  • 会话对会话启动时使用的协议(http、https)敏感。为http://example.com 设置的会话与https://example.com 的会话不同 请确保您没有在某处更改协议。
  • @DFriend 不,我尝试不同。我跟进了许多建议和修复,但没有运气。只是不适用于 http 并且无法弄清楚为什么,我整晚都在这上面。
  • 没有代码检查是不可能提供更多帮助的。
  • @DFriend 请查看问题 EDIT v2。添加了一些代码,如果您需要其他任何内容,请告诉我。谢谢!
  • hkr_sessions 表设置是否与here 指定的一样,在“ip_address”字段上有一个主键?当sess_match_ip = TRUE 时,这是必需的。您是否尝试过sess_match_ip = FALSE 看看这是否有所作为?

标签: php codeigniter session


【解决方案1】:

我实际上是在修改我的 .htaccess 文件上的标题,而一个规则导致了这个问题。请参阅注释行 - 该行是问题所在。

<IfModule mod_headers.c>
    Header append Vary User-Agent env=!dont-vary
    Header set X-XSS-Protection: "1; mode=block"
    Header unset Server
    Header set X-Content-Security-Policy "allow 'self';"
    # Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2017-04-19
    • 2019-01-29
    • 2017-01-27
    • 2012-11-20
    • 2012-03-17
    • 2019-10-13
    相关资源
    最近更新 更多