Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Run A Script When Expires A Session
TavoxPeru
post Nov 25 2007, 02:06 AM
Post #1


Super Member
Group Icon

Group: [HOSTED]
Posts: 746
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



For example, when a user logins to a page -with a login form- and after validating and verifying it's credentials i store some information related to this user in session variables and a table with his state -connected- is updated, then i use it in other pages, etc.

When this user logouts -by clicking a logout link- i release -unregister, destroy, etc- all the session variables stored and the same table is updated with his state -disconnected-.

All of this funcionality works very well, the problem comes when the user do not click on the logout link and the session expires, the user state is not updated because of this and remains connected. How can i run a php script when a session expires??? Is it possible to do this???

Best regards,
Go to the top of the page
 
+Quote Post
Jimmy89
post Nov 25 2007, 09:43 AM
Post #2


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 30-June 06
From: Australia
Member No.: 14,219



you could possibly run a cron job every 15-20 minutes (i know that puts strain on the gamma server) that checks if there has been any activity by logged in users, if not, run their expiry script! you may want to run it more frequently to keep the information more up to date, but that could be a huge strain on the web server, if you run it for users that are 'supposed' to be logged in, that would be better then running it for all users.

This post has been edited by Jimmy89: Nov 25 2007, 09:43 AM
Go to the top of the page
 
+Quote Post
pyost
post Nov 25 2007, 11:19 AM
Post #3


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,002
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500



A Cron Job would be the easiest, but not the most efficient solution, as it may be an overkill for the server.

First of all, you would have to have a MySQL column indicating the time of the last activity. If you run the script every 15 minutes, let's say you want to log out users that haven't done anything during those 15 minutes. Supposing that you store that last activity time as a UNIX timestamp in the database, this would be the simplest query:

CODE
$query = 'SELECT `what_you_need` FROM `table` WHERE `logged_in` = 1 AND `last_act` + 900 < '.time();


After that, all you have to do is log out each result row.

You may also want to have a look at how different bulletin boards function. IP.Board, for example, doesn't use Cron Jobs at all, but has "user(s) active in the past 15 minutes". Since there are many big forums using this software, the script it uses obviously isn't too greedy smile.gif
Go to the top of the page
 
+Quote Post
vujsa
post Nov 25 2007, 05:54 PM
Post #4


Absolute Newbie
Group Icon

Group: Admin
Posts: 887
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714



I'm assuming that you store your session data in a database.

Instead of using a CRON job, I suggest placing code in you main script to run every time a page is requested. This way the next user activates the script that cleans up your session table. Like so:
SQL
UPDATE table_session SET active = 0 WHERE session_time < UNIX_TIMESTAMP() - 20*60 AND active = 1

Update all active records in table_session to inactive where session_time is less than the the current time minus 20 minutes.

This will add one additional SQL query to each page load but will provide you with an auto-logout system for users that don't log out.

You could also reset the last session time to when the user should have been logged out...
SQL
UPDATE table_session SET active = 0, session_time = session_time + 20 * 60 WHERE session_time < UNIX_TIMESTAMP() - 20*60 AND active = 1

This way you would be able to see when the user was logged out if you care.

Be sure you limit the search query to active sessions only to reduce server load and place an index on that column would assist the server further. By using the UPDATE query, the script does two jobs at once, it looks for expired sessions and updates the information as needed. There is no need to run a SELECT query in this case since you should need to return any data at this time and the items you would select are the ones that need to be updated anyway. UPDATE do its thing and if there are no records to update then it will just return zero rows affected...

Hope this helps. cool.gif
vujsa
Go to the top of the page
 
+Quote Post
TavoxPeru
post Nov 26 2007, 07:26 AM
Post #5


Super Member
Group Icon

Group: [HOSTED]
Posts: 746
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



First of all, thanks everybody for your really helpful information.

And second, based on this information i decide to follow the suggestions made by vujsa to resolve this issue because in my opinion:
  • The php code will be very simple to develop and efficient.
  • Don't deal with cron jobs.
  • Don't overkill the server.
Best regards,
Go to the top of the page
 
+Quote Post
vujsa
post Nov 26 2007, 08:32 PM
Post #6


Absolute Newbie
Group Icon

Group: Admin
Posts: 887
Joined: 20-February 05
From: Indianapolis, Indiana, USA (Midwest)
Member No.: 2,714



QUOTE(TavoxPeru @ Nov 26 2007, 02:26 AM) *
First of all, thanks everybody for your really helpful information.

And second, based on this information i decide to follow the suggestions made by vujsa to resolve this issue because in my opinion:
  • The php code will be very simple to develop and efficient.
  • Don't deal with cron jobs.
  • Don't overkill the server.
Best regards,

YAY, I WIN!!!!!!!!!!!!!!!!laugh.gif

Glad I could help. I actually answered this topic twice. blink.gif
I wrote a nice reply then decided not to submit it because I wasn't sure if you were storing your session data in a database. If you were storing all of the information in the server's session path, then the process is much different and I'm not sure how I would do it much less try and explain to you how to do it. Then after reading the other replies, I decided to go ahead and reply.

I'm a huge fan of having what ever user is online currently trigger global system events like this. Of course, if you site isn't very busy then it won't work very well but if it isn't busy, then there would be anyone there to notice that everyone is still logged in. laugh.gif

Good Luck,

vujsa
Go to the top of the page
 
+Quote Post
TavoxPeru
post Dec 5 2007, 02:19 AM
Post #7


Super Member
Group Icon

Group: [HOSTED]
Posts: 746
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(vujsa @ Nov 26 2007, 03:32 PM) *
YAY, I WIN!!!!!!!!!!!!!!!!laugh.gif

Glad I could help. I actually answered this topic twice. blink.gif
I wrote a nice reply then decided not to submit it because I wasn't sure if you were storing your session data in a database. If you were storing all of the information in the server's session path, then the process is much different and I'm not sure how I would do it much less try and explain to you how to do it. Then after reading the other replies, I decided to go ahead and reply.

I'm a huge fan of having what ever user is online currently trigger global system events like this. Of course, if you site isn't very busy then it won't work very well but if it isn't busy, then there would be anyone there to notice that everyone is still logged in. laugh.gif

Good Luck,

vujsa

Well i try both ways, storing in a database -which is the one i currently use- and storing all the information in the server's session path, could you explain how to do it with the second one???? maybe it will be more efficient, do you have test both of them???

Best regards,
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Make A Text Based Online Game Script ?(23)
  2. PHP: Writing A Generic Login And Register Script(14)
  3. Php Script To Download File From Another Site(9)
  4. Coppermine Random Image Script(6)
  5. How To Save A Image In Pdf File And Download It?(10)
  6. Bash Script To Display Your Ip(9)
  7. Help Needed To Create Login Script With Perl/cgi(21)
  8. Need Help With A PHP - MySQL Registration Script(13)
  9. PHP Tutorial: Form Verification And Simple Validation(12)
  10. Need Help - How To Remove Session ID From URL(6)
  11. Create And Import JavaScript Modules For A Large Script(2)
  12. Running Vba Script In Excel(6)
  13. Rpg For (php)wml Script Text Based(1)
  14. Disable Crash Recovery Restore Session Option On Firefox 2(3)
  15. Very Simple Login-script(18)
  1. Attack Script In Php(5)
  2. A Simple Register Script(3)
  3. Auto-click Script(7)
  4. Script Request(2)
  5. Love Script(3)
  6. Myspace Gold Script(2)
  7. Looking For Script(5)
  8. Free Login Script(1)
  9. Php Login Script(0)
  10. Php Location Header No Send Session Id ?(0)
  11. Session Issues(1)
  12. Creating A Php Login Script(3)
  13. Myspacetv Download Php Script Help(6)


 



- Lo-Fi Version Time is now: 5th September 2008 - 01:10 PM