I am using an Ajax call to update a users data and to also store the value in the session.
我正在使用Ajax调用来更新用户数据并将值存储在会话中。
(Please note I am not talking about storing sessions in databases here).
(请注意,我不是在谈论在数据库中存储会话)。
In the ajax controller I make a call like this:
在ajax控制器中,我进行如下调用:
$this->session->userdata['user']['email'] = 'some_new_email@blah.com';
and then to test that it has worked I say:
然后测试它是否有效我说:
print_r($this->session->all_userdata());
and I can clearly see that the session has been updated as expected.
我可以清楚地看到会话已按预期更新。
However, once I have returned from the ajax controller I then say:
但是,一旦我从ajax控制器返回,我就说:
print_r($this->session->all_userdata());
and the value of the session variable which I have just updated has returned to its previous (old) state.
我刚刚更新的会话变量的值已返回其先前(旧)状态。
QUESTION:
Why oh why is this happenning and how do I fix it?
为什么哦为什么会这样,我该如何解决?
Any suggestions welcome..
欢迎任何建议..
CODE:
1) jquery ajax call posts user data to here: The data is then sent to the api where it is validated (again) and then updated in the database. $response is the result of the api call (the updated user object in json format):
1)jquery ajax调用将用户数据发布到此处:然后将数据发送到api,在那里验证(再次),然后在数据库中更新。 $ response是api调用的结果(以json格式更新的用户对象):
$response = $account_repository->update_email($update_email_entity);
if($response['status']['code']==200)
{
// [Update users email in database was a success, now update session to reflect changes]:
$this->session->userdata['users']['email'] = $response['payload']['email'];
print_r($this->session->all_userdata());
}
else ...
2) The print_r above shows that the session has been updated correctly. After the ajax has returned I then refresh the page (there is a print_r on the page which contains all of the users session data). The session data has not been updated.
2)上面的print_r表明会话已正确更新。在ajax返回后,我刷新页面(页面上有一个包含所有用户会话数据的print_r)。会话数据尚未更新。
INFO:
My ajax controller function is just another function in the account controller. Other functions in this same controller make updates to the session and everything works fine. It is just when the controller function is accessed via ajax that I have this problem.
我的ajax控制器功能只是帐户控制器中的另一个功能。同一控制器中的其他功能会对会话进行更新,一切正常。只是当通过ajax访问控制器功能时我才遇到这个问题。
Has anyone seen this problem before? Any suggestions on fixing it?
有没有人见过这个问题?有关修复它的任何建议吗?
1 个解决方案
#1
0
Maybe session_regenerate_id is your problem. Your XHR call regenerate the sessionId without deleting the old session file. The next call works with old session_id by cookie.
也许session_regenerate_id是你的问题。您的XHR调用会重新生成sessionId而不删除旧的会话文件。下一个调用与旧的session_id一起使用cookie。
For example.. When an AJAX is done, sessionId is updated and a new one is generated and maybe updated in the database but the cookie in the browser is not updated, so the next time browser requests a new page it sends a cookie with the wrong session id and the session becomes invalid or retrieved old data.
例如..当AJAX完成时,会更新sessionId并生成一个新的,并且可能在数据库中更新,但浏览器中的cookie未更新,因此下次浏览器请求新页面时会发送一个cookie,其中包含错误的会话ID和会话变得无效或检索旧数据。
#1
0
Maybe session_regenerate_id is your problem. Your XHR call regenerate the sessionId without deleting the old session file. The next call works with old session_id by cookie.
也许session_regenerate_id是你的问题。您的XHR调用会重新生成sessionId而不删除旧的会话文件。下一个调用与旧的session_id一起使用cookie。
For example.. When an AJAX is done, sessionId is updated and a new one is generated and maybe updated in the database but the cookie in the browser is not updated, so the next time browser requests a new page it sends a cookie with the wrong session id and the session becomes invalid or retrieved old data.
例如..当AJAX完成时,会更新sessionId并生成一个新的,并且可能在数据库中更新,但浏览器中的cookie未更新,因此下次浏览器请求新页面时会发送一个cookie,其中包含错误的会话ID和会话变得无效或检索旧数据。