无法使用CodeIgniter中的会话类检索会话ID

meh*_*hdi 2 session codeigniter

我在我的控制器中有这个代码

class Upload_center extends Controller 
{
    function __construct()
    {
        parent::Controller();
        $this->load->model('auth_model') ;
        if(!$this->auth_model->authorize())
        {
            redirect('login');

        } 

    }
Run Code Online (Sandbox Code Playgroud)

我在我的视图中有这个代码

$('#swfupload-control').swfupload({
    upload_url: "<?=base_url()?>index.php/upload_center/upload",
    file_post_name: 'fileupload',
    file_size_limit : "200000000",
    file_types : "*.zip;*.rar;*.pdf;*.doc;*.docx;*.mp3;*.avi;*.wmv;*.docx;*.jpg;*.jpeg;*.JPG;*.JPEG;*.png;*.gif;*.bitmap;",
    file_types_description : "zip files ",
    post_params: {"PHPSESSID": "<?=$this->session->userdata('session_id');?>"} ,
    file_upload_limit : 1,
    flash_url : "<?=base_url()?>js/jquery-swfupload/js/swfupload/swfupload.swf",
    button_image_url : '<?=base_url()?>js/jquery-swfupload/js/swfupload/wdp_buttons_upload_114x29.png',
    button_width : 114,
    button_height : 29,
    button_placeholder : $('#button')[0],
    debug: false
Run Code Online (Sandbox Code Playgroud)

我希望用户在登录后上传他们的文件,所以我有一个方法需要用户登录才能继续上传文件.虽然我在我的视图中使用了flash uploader,但我认为它没有传递会话值,而且PHP认为用户没有登录并且将其重定向到登录页面.我phpsessionid通过邮寄发送但仍然没有.

tre*_*ace 7

你的会话cookie到期时间设置为什么?CodeIgniter中存在一个已知错误,如果您停留在使AJAX请求超过cookie过期的页面上,它将重置数据库中的会话ID,但无法在浏览器cookie中设置它,因为它是异步的请求.这导致在下一个非异步GET请求上断开连接,导致会话库调用sess_destroy().如果这听起来像你的情况,请告诉我.否则,请提供更多细节.

编辑:我或许也应该在这里包含对此bug的修复.在/ application/libraries中创建一个名为"MY_Session.php"的文件(如果它尚不存在).在那里你可以粘贴这个:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Session Class Extension
*/
class MY_Session extends CI_Session {
   /*
    * Do not update an existing session on ajax calls
    *
    * @access    public
    * @return    void
    */
    function sess_update() {
        if ( !isAjax() ){
            parent::sess_update();
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

那个isAjax()函数是我在/application/helpers/isajax_helper.php中的一个帮助,它看起来像这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* is_ajax_call
*
* Determines if the current page request is done through an AJAX call
*
* @access    public
* @param    void
* @return    boolean
*/
if ( ! function_exists('isAjax')) {
    function isAjax() {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

在我的配置文件中引用了这样的:

$autoload['helper'] = array('otherhelper1', 'isajax', 'otherhelper2');
Run Code Online (Sandbox Code Playgroud)

  • 你现在可以[从2.0.x开始]使用`$ this-> input-> is_ajax_request()`而不是创建`isAjax()`. (5认同)