【发布时间】:2018-03-25 01:05:24
【问题描述】:
我有一个问题希望你能帮助我。我的控制器中有一个创建 CodeIgniter 会话的方法。为了确保创建正确,我在创建会话后执行print_r() 函数。然后,在我的 Ajax 请求中,我输入了 location.reload 来刷新页面。
这就是我的方法:
public function CreateSession() {
$response = $this->mymodel->registerUser();
//The response in this case of the method registerUser() is an array such as:
/*
Array
(
[id_web_user] => 982
[nombre_web_user] => name
[apellidos_web_user] => lastname
[user_mail] => asf21@gmail.com
[user_password] => 202cb962ac59075b964b07152d234b70
[id_pais] => 1
[profile_picture] => n/a
[id_seccion_registro] => 1
[id_estatus] => 0
[id_usuario_alta] => 1
[id_usuario_mod] => 1
[fecha_ultimo_ingreso] => 2018-03-25 01:43:17
[fecha_alta] => 2018-03-25 01:43:17
[fecha_modificacion] => 2018-03-25 01:43:17
)
*/
//Then when I get the response array, I create a new array which contains an specific data for store in a session within userdata like:
$newdata = array(
'id_web_user' => $response['id_web_user'],
'nombre_web_user' => $response['nombre_web_user'],
'apellidos_web_user' => $response['apellidos_web_user'],
'user_mail' => $response['user_mail'],
'profile_picture' => $response['profile_picture']
);
//Finally I create the session:
$this->session->set_userdata($newdata);
//And then, wheter I do a print_r($this->session->userdata) for verify that the session was created successfully, I get something like:
/*
Array
(
[__ci_last_regenerate] => 1521938249
[id_web_user] => 983
[nombre_web_user] => name
[apellidos_web_user] => lastname
[user_mail] => asf21@gmail.com
[profile_picture] => n/a
)
*/
//In this step I do an echo 1; because my Ajax request is waiting for that and make a "location.reload()", so:
echo 1;
}
这个方法通过Ajax调用,比如;
//base_url was defined previously
$.ajax({
type : "POST",
url : base_url + "/mycontroller/CreateSession",
data : {},
success : function(response) {
if ( response == 1 ) {
location.reload();
}
}
});
所以,最后,在 Ajax 请求中,我执行 location.reload,但是当页面刷新时,这会将我重定向到 http://localhost/,并且当我想检查包含 $this->session->userdata 和 print_r($this->session->userdata); 的内容时,这是空的。
我做错了什么?我试图找到一个解决方案,有人说 CI 的 Session.php 有一个错误,需要替换 CI 的 Github 中存在的新的。其他人说这是 cookie 的问题,因为当会话包含超过 4KB 时,它会自动擦除。这太疯狂了,我希望你能帮助我。非常感谢您的支持。
【问题讨论】:
-
你是否在config.php上设置了会话保存路径不要让它为空。
-
你使用的是 ci 2 对吗?
-
在所有启用 session.autostart with 1 之后问题就解决了
标签: javascript php ajax codeigniter