简体   繁体   English

Codeigniter-会话数据未通过数组保存在数据库中

[英]Codeigniter - Session data not saving in database via array

When I enter my credentials for my login program developed in Codeigniter the sessions don't seem to be saving in the ci_sessions table in the MySQL database. 当我输入在Codeigniter中开发的登录程序的凭据时,会话似乎没有保存在MySQL数据库的ci_sessions表中。

Once the form is filled in, the session will be set in the controller.php (sample code below): 填写表格后,会话将在controller.php中设置(以下示例代码):

if ($this->form_validation->run()) {
    $data = array(
       'email' => $this->input->post('email'), // posting the email input field
       'is_logged_in' => 1 
    );

$this->session->set_userdata($data);  

redirect('main/members');

} else {

    $this->load->view('login');

}  

At the moment its outputting the following when executing: 目前在执行时输出以下内容:

Array
(
   [__ci_last_regenerate] => 1440877455
   [email] => jhon@gmail.com
   [is_logged_in] => 1
)

The output is using the following code in view.php 输出在view.php中使用以下代码

print_r($this->session->all_userdata());

The array should be outputting something similar below but its not: 该数组应该在下面输出类似的内容,但不是:

Array
(
   [session_id] => dd7e0e2266da6481ef45faaa7e3c808b
   [ip_address] => 127.0.0.1
   [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
   [last_activity] => 1387619782
   [user_data] => 
   [username] => Jhon
   [email] => jhon@gmail.com
)

config.php: config.php文件:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

autoload.php autoload.php

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

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

$autoload['helper'] = array('url', 'form', 'html', 'security');

I have checked the config.php to ensure everything is set correctly and referred to the manual on Codeigniters website but still no joy in solving the issue. 我已经检查了config.php以确保一切设置正确,并参考了Codeigniters网站上的手册,但在解决问题上仍然不满意。

Any idea's as to why this is happening? 是否知道为什么会这样? OR Any possible solutions? 或任何可能的解决方案?

There's a number of problems here ... 这里有很多问题...

At the moment its outputting the following when executing: 目前在执行时输出以下内容:

\nArray 排列\n( \n  [__ci_last_regenerate] => 1440877455 [__ci_last_regenerate] => 1440877455\n  [email] => jhon@gmail.com [电子邮件] => jhon@gmail.com\n  [is_logged_in] => 1 [is_logged_in] => 1\n) \n

... ...

The array should be outputting something similar below but its not: 该数组应该在下面输出类似的内容,但不是:

\nArray 排列\n( \n  [session_id] => dd7e0e2266da6481ef45faaa7e3c808b [session_id] => dd7e0e2266da6481ef45faaa7e3c808b\n  [ip_address] => 127.0.0.1 [ip_address] => 127.0.0.1\n  [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 [user_agent] => Mozilla / 5.0(Windows NT 6.1)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 31.0.1650.63 Safari / 537.36\n  [last_activity] => 1387619782 [last_activity] => 1387619782\n  [user_data] => [user_data] => \n  [username] => Jhon [用户名] => Jhon\n  [email] => jhon@gmail.com [电子邮件] => jhon@gmail.com\n) \n

No it shouldn't ... you're not setting "username" anywhere and the rest is explained in the manual . 不,不应该...您没有在任何地方设置“用户名”,其余内容在手册中进行了说明


Let's inspect your configuration now ... 让我们现在检查您的配置...

$config['sess_driver'] = 'files';

^ This line is what tells the library what storage to use. ^这行告诉图书馆要使用什么存储。 If it is set to 'files', you cannot expect sessions to be saved to the database ... 如果将其设置为“文件”,则不能期望将会话保存到数据库中。

$config['sess_encrypt_cookie'] = FALSE;

^ This setting is obsolete; ^此设置已过时; it does nothing on CI 3.0.x. 在CI 3.0.x上不执行任何操作。

$config['sess_use_database'] = TRUE; $ config ['sess_use_database'] = TRUE;

^ Since you have set $config['sess_driver'] , this setting is ignored - remove it. ^由于已设置$config['sess_driver'] ,因此将忽略此设置-将其删除。

$config['sess_table_name'] = 'ci_sessions';

^ This is also ignored, and you shouldn't be using it with CI3. ^这也将被忽略,并且您不应该将其与CI3一起使用。 Set $config['sess_save_path'] instead. 改为设置$config['sess_save_path']

$config['sess_save_path'] = NULL;

^ You're not supposed to leave this to NULL. ^您不应将此设置为NULL。 Both the manual and the inline comments in the stock config.php file say that setting this is REQUIRED! 库存config.php文件中的手册和内联注释都要求设置此设置!

$config['sess_match_useragent'] = FALSE;

^ This also does nothing in CI3. ^这在CI3中也不起作用。

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

^ Nothing in the documentation suggests this and it does nothing. ^文档中没有任何内容暗示这一点,并且它什么也没有做。


I have checked the config.php to ensure everything is set correctly and referred to the manual on Codeigniters website but still no joy in solving the issue. 我已经检查了config.php以确保一切设置正确,并参考了Codeigniters网站上的手册,但在解决问题上仍然不满意。

On the contrary, you didn't pay attention even to the most basic documentation, which are short descriptions in the config files ... You've obviously just copy-pasted your old settings from CodeIgniter 2.x and didn't follow the upgrade instructions . 相反,您甚至没有关注最基本的文档,这些文档是配置文件中的简短描述……您显然只是从CodeIgniter 2.x复制粘贴了您的旧设置,并且没有遵循升级说明

Here is what i will advice you to do 这是我会建议你做的

$data = array(
       'email' => $this->input->post('email'), // posting the email input field
       'is_logged_in' => 1 
    );
//Use Var_dump($data) directly under the code above and add a die statement to check if the array actually contain the data as specified above
$this->session->set_userdata($data); 

/*
** store the session inside a variable then var_dump it if it is really 
* in there
*/

$result = $this->session->set_userdata($data);
var_dump($result);die;

Note the changes and try to replicate samething just to confirm if those data are actually stored in an array and also to check if the session is available inside the $result 注意所做的更改,并尝试复制相同内容以确认那些数据是否确实存储在数组中,并检查会话是否在$ result中可用

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

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