[英]CodeIgniter is not saving session trough AJAX
我有一個問題,希望您能對我有所幫助。 我的控制器中有一個創建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/
,並且當我想用print_r($this->session->userdata);
來檢查包含$this->session->userdata
時print_r($this->session->userdata);
這是空的。
我做錯了什么? 我試圖找到一種解決方案,有人說CI的Session.php有一個錯誤,需要替換CI的Github中存在的新錯誤。 其他人說這是cookie的問題,因為當會話包含4KB以上時,它會自動清除。 這很生氣,希望您能幫助我。 非常感謝您的支持。
您是否將此庫稱為$ this-> load-> library('session');
或在application / config / autoload中調用此庫。 php文件使用... $ autoload [“ libraries”] = array(“ session”);
感謝您的回答。 我解決了我的問題,但有一些限制。 似乎CodeIgniter無法在會話的數組中存儲大量數據,因此我以官方文檔中存在的創建會話為例。
我以以下示例為例: https : //www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe@some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
因此,在上面的代碼示例中,我基於此構建了一個新的自定義會話。 然后,我修改了我之前的代碼部分:
$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);
對於
$id = $response['id_web_user'];
$username = htmlspecialchars(sprintf('%s %s', $response['nombre_web_user'], $response['apellidos_web_user']));
$newdata = array(
'username' => $username,
'id_web_user' => $id,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
請看一下,根據我的上一個代碼,我僅從$response
數組中獲取了三個值來構建自定義會話,它們是:
$response['id_web_user']
$response['nombre_web_user']
$response['apellidos_web_user']
因此,我僅采用該樹值,並將其分別保存在各自的變量中,這些變量現在為:
$id = $response['id_web_user'];
$username = htmlspecialchars(sprintf('%s %s', $response['nombre_web_user'], $response['apellidos_web_user']));
請看一下,我在數組中添加了第三個值(作為CodeIgniter示例),該值是'logged_in' => TRUE
。
這對我有用。
結論:我不得不說,在互聯網上找到解決方案對我來說永遠行不通。 我做的第一件事是在Google上進行搜索,為什么調用創建會話通過AJAX的方法的方法不起作用,我發現了兩種可能性:
system/Sessions.php
。 此解決方案對我而言從來沒有用過,因為此人在兩個參考文獻中建議的文件都與我的CodeIgniter文件不匹配。 參考頁:
https://forum.codeigniter.com/thread-68010.html
參考頁:
我希望我的回答足以讓我告訴您什么以及如何解決我的問題。 如果此答案中缺少某些內容,請告訴我進行更新。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.