首页 文章

CodeIgniter无法在db上找到会话userdata

提问于
浏览
0

我有一个c#客户端应用程序,它使用不同的URL执行两个Web请求,发送一个json字符串并接收要反序列化的json字符串 . 这是他们的代码:

private CookieContainer cookie = new CookieContainer();
public string AskInfo(string data, string url) {
        string result = null;
        try {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.CookieContainer = cookie;
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Accept = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
                streamWriter.Write(data);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
                result = streamReader.ReadToEnd();
            }
        } catch (Exception e){
            logger.Debug("Error while asking infos from server", e.Message);
        }
        return result;
    }

这是我的config.php文件:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

我的控制器有这个构造函数:(我也尝试使用相同的结果自动加载会话库)

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('session');
}

这是我的控制器方法的代码:在第一次调用:http://mywebserver/mycontroller/offerupdate/,代码如下:

public function offerUpdate() {
    $data = json_decode(file_get_contents('php://input'), true);
        $session_data = array(
            'fileName' => 'prova.exe',
            'member' => $data['member'],
            'versions' => $data['versions']
        );
        $this->session->set_userdata($session_data);
        log_message('history', 'Offer Update: Session has user data: '.$this->session->has_userdata('member'));

        $response['updatable'] = TRUE;
        $response['file'] = $this->session->userdata('fileName');
        header('Content-Type: application/json');
        echo json_encode($response);
}

在第二个:http://mywebserver/mycontroller/offerMD5使用此代码:

public function offerMD5() {
    $data = json_decode(file_get_contents('php://input'), true);
    log_message('history', 'Offer MD5: Session has user data: '.$this->session->has_userdata('member'));
    $file = BASEPATH.$data['file'];
    $response['MD5'] = md5_file($file);
    header('Content-Type: application/json');
    echo json_encode($response);
}

这是我的问题:为什么我的 Logger 打印:

HISTORY - date, time --> Offer Update: Session has user data: 1
HISTORY - date, time --> Offer MD5: Session has user data:

这个文件“提供MD5:会话有用户数据:”只是空,不是真(1)也不是假(0),如上所述 . 实际上CodeIgniter不能告诉我我需要的数据是否存在 . 我想CodeIgniter不会关闭会话,因为它仍然在我的数据库中 . 那么,为什么我有 Logger 记录?一切正常,但会话的用户数据没有 . 似乎我遗漏了一些东西,但我无法理解 .

EDITED: 这是探查器输出:

INFO - 2017-04-13 10:09:33 --> Config Class Initialized
INFO - 2017-04-13 10:09:33 --> Hooks Class Initialized
DEBUG - 2017-04-13 10:09:33 --> UTF-8 Support Enabled
INFO - 2017-04-13 10:09:33 --> Utf8 Class Initialized
INFO - 2017-04-13 10:09:33 --> URI Class Initialized
INFO - 2017-04-13 10:09:33 --> Router Class Initialized
INFO - 2017-04-13 10:09:33 --> Output Class Initialized
INFO - 2017-04-13 10:09:33 --> Security Class Initialized
DEBUG - 2017-04-13 10:09:33 --> Global POST, GET and COOKIE data sanitized
INFO - 2017-04-13 10:09:33 --> Input Class Initialized
INFO - 2017-04-13 10:09:33 --> Language Class Initialized
INFO - 2017-04-13 10:09:33 --> Loader Class Initialized
INFO - 2017-04-13 10:09:33 --> Controller Class Initialized
INFO - 2017-04-13 10:09:33 --> Helper loaded: url_helper
INFO - 2017-04-13 10:09:33 --> Database Driver Class Initialized
INFO - 2017-04-13 10:09:33 --> Session: Class initialized using 'database' driver.
INFO - 2017-04-13 10:09:33 --> Final output sent to browser
DEBUG - 2017-04-13 10:09:33 --> Total execution time: 0.0312

然后是第二个Web请求:

INFO - 2017-04-13 10:09:35 --> Config Class Initialized
INFO - 2017-04-13 10:09:35 --> Hooks Class Initialized
DEBUG - 2017-04-13 10:09:35 --> UTF-8 Support Enabled
INFO - 2017-04-13 10:09:35 --> Utf8 Class Initialized
INFO - 2017-04-13 10:09:35 --> URI Class Initialized
INFO - 2017-04-13 10:09:35 --> Router Class Initialized
INFO - 2017-04-13 10:09:35 --> Output Class Initialized
INFO - 2017-04-13 10:09:35 --> Security Class Initialized
DEBUG - 2017-04-13 10:09:35 --> Global POST, GET and COOKIE data sanitized
INFO - 2017-04-13 10:09:35 --> Input Class Initialized
INFO - 2017-04-13 10:09:35 --> Language Class Initialized
INFO - 2017-04-13 10:09:35 --> Loader Class Initialized
INFO - 2017-04-13 10:09:35 --> Controller Class Initialized
INFO - 2017-04-13 10:09:35 --> Helper loaded: url_helper
INFO - 2017-04-13 10:09:35 --> Database Driver Class Initialized
INFO - 2017-04-13 10:09:35 --> Session: Class initialized using 'database' driver.
INFO - 2017-04-13 10:09:35 --> Final output sent to browser
DEBUG - 2017-04-13 10:09:35 --> Total execution time: 0.0780

看起来codeigniter会在每次请求时重启所有内容 . 现在我认为我使用错误的逻辑来实现这一目标

1 回答

  • 0

    只是几个想法,尝试设置

    $config['sess_match_useragent']=FALSE;
    

    我听说过浏览器没有按照cookie应该做的事情 .

    尝试启用探查器以查看会话发生的情况,因为默认情况下隐藏信息 .

    $this->output->enable_profiler(TRUE);
    

    除此之外,我几乎认为CodeIgniter论坛对于您可能想要搜索的这个特定问题来说是比SO更好的资源 .

相关问题