• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI3 Ajax and Sessions expiration

#1
Hello everyone,
I'm using CI for a while now and I decided to use it for one of my semestral projects. My problem is something I haven't encountered before. I'm using sessions for login and other parts of the application and my app uses AJAX call in loop (let's say 1 minute loop) to refres data. I'm having a problem with Sessions expiring sooner than I would expect. Even when I set them to last at least a day, they expire after aroun 2 mines.
I googled a little bit and found that this was some kind of bug back in CI2 and that it was fixed. However I did not find very much about this topic considering CI3.
I'm using 
Is there any way to fix this problem? 

Thank you very much in advance!


-------
How the app works.:
(View AJAX avery minute) => (Controller: check if user is logged + call to a DB for data + return data and display)

Update:
No it seems that the problem is present only when "sess_time_to_update" is set to low number - like the default 300. When I set it to long period of time (say a day) the problem seems not to occur anymore.
Reply

#2
(01-11-2016, 12:02 PM)Novotmike Wrote: Hello everyone,
I'm using CI for a while now and I decided to use it for one of my semestral projects. My problem is something I haven't encountered before. I'm using sessions for login and other parts of the application and my app uses AJAX call in loop (let's say 1 minute loop) to refres data. I'm having a problem with Sessions expiring sooner than I would expect. Even when I set them to last at least a day, they expire after aroun 2 mines.
I googled a little bit and found that this was some kind of bug back in CI2 and that it was fixed. However I did not find very much about this topic considering CI3.
I'm using 
Is there any way to fix this problem? 

Thank you very much in advance!


-------
How the app works.:
(View AJAX avery minute) => (Controller: check if user is logged + call to a DB for data + return data and display)

Update:
No it seems that the problem is present only when "sess_time_to_update" is set to low number - like the default 300. When I set it to long period of time (say a day) the problem seems not to occur anymore.


CodeIgniter and working with Ajax calls:

1) The function in the controller must check if it is an HTTP POST request, using $this->input->post(). You can also check if it is an AJAX request using $this->input->is_ajax_request().
2) The above function will return data in text format - can be html, json or simply plain text. (This will be the response in step 4)
3) JS function you use to execute the controller's function should send HTTP POST, instead of HTTP GET. (I use jQuery .post()) 
4) The response received from HTTP POST can be processed to can determine whatever action to be done.
5) On Ajax calls, do a call to $this->session_write_close();

PHP Code:
if ($this->input->is_ajax_request())
{
 
   $this->session_write_close();
}
else
{
 
   // Normal call


Yoou may need to do the $this->session_write_close(); before the Ajax check.

Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(01-11-2016, 02:40 PM)InsiteFX Wrote: 1) The function in the controller must check if it is an HTTP POST request, using $this->input->post(). You can also check if it is an AJAX request using $this->input->is_ajax_request().

What if it's a GET request?

(01-11-2016, 02:40 PM)InsiteFX Wrote: 3) JS function you use to execute the controller's function should send HTTP POST, instead of HTTP GET. (I use jQuery .post()) 

Nonsense, GET requests work just fine if that's what you want.
In fact, if you're polling for data from the server (which is what the OP is doing) instead of submitting to it, using POST would be semantically wrong.

(01-11-2016, 02:40 PM)InsiteFX Wrote: 5) On Ajax calls, do a call to $this->session_write_close();

This will trigger a fatal error ... session_write_close() is a PHP function, not a method declared by any CI class.
Reply

#4
See what happens when you copy and past an answer from someone else!
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
Thank you very much for your responses. I might think about using POST instead of GET. It isn't that bay since I have to pass some values such as userID. I will have to set up CSRF though.
I'll try it out and tell you how it went. For not it works when I made the sess_time_to_update larger. Is it a problem having it large number such as day?

Thank you.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.