Jump to content

[SOLVED] Logging out after a certain time.


HappyPandaFace

Recommended Posts

Depends on what you mean by log out (you just want them not to appear in a "currently online" list after X minutes, or do you want them to actually have to type their user/pass in again) and depends on how you keep people logged in (sessions or cookies).  You need to phrase the question better.

Link to comment
Share on other sites

Ok, I'm actually running php as a side script with flash. Sorry I forgot to mention that. I need it to have the mySQL database log out the person on its own, so if they close the program it will not continue to say they are logged in. I don't think you need to know much about flash to solve this problem.

Link to comment
Share on other sites

Hi,

 

  use cookie, like what other web apps do, when you check  the box,  remember me, this will save some

cookie values on the client computer, so when you open your the site again you don't need to re login.

 

basically the md5 hash of the password and username is saved in the cookie, and when the user goes to

the site again, it will read the cookie, and used the values to log you in.

 

Good luck.

Link to comment
Share on other sites

I rellay don't thank that's going to work for my situation. I'm trying to make it so if they close the program or something it still counts down for a time limit to log out. Even when no php script are running, then when it gets to zero it changes a variable in my mySQL server and other people read this and it says he's not logged in.

Link to comment
Share on other sites

set the cookie expiration to what you need, say ... every refresh the cookie will be set to 1 hour,

if they close the apps and open again after 30 mins they are still logged in, if it is more than an hour,

the cookie has already expired.

 

In that way it still makes a count down

Link to comment
Share on other sites

I rellay don't thank that's going to work for my situation. I'm trying to make it so if they close the program or something it still counts down for a time limit to log out. Even when no php script are running, then when it gets to zero it changes a variable in my mySQL server and other people read this and it says he's not logged in.

This requires the use of a database.

Here's a simple breakdown so you can get the gist of what needs to be done:

 

You have a database table, let's call it tbl_sessions for now.  In tbl_sessions, there is a column called last_time.  Every time the user opens a new page on your site, last_time is set to the time() that that page was opened.  If a user doesn't open a page for say...30 minutes, last_time will obviously be set to a time 30 minutes ago.  So, when a user goes to the page to see who is online, your query basically says:

Select everything from tbl_sessions where the column last_time is GREATER THAN time() - 30 minutes.  I'm too lazy to write the SQL for that, although it's extremely simple...oh well, here it is:

$timeFrom = time() - (30 * 60);
$sql = "SELECT * FROM tbl_sessions WHERE last_time > $timeFrom";

That will return only the users who have opened a page in the last 30 seconds.  Now, as for assigning a user to a specific row in a table, that's where the cookies come in.  You have a column in tbl_sessions that is session_id.  In that session_id column, for each user that visits the site, a random 32 character code is generated.  That code is stored in session_id, and is also written to a cookie on the client's computer.  When the client returns to the site, your PHP scripts read the cookie, find that session_id, pull the information from tbl_sessions, and updates the last_time.

Link to comment
Share on other sites

To add to Glyde's good suggestion, if you have a high volume of users you may want to make it so that the check is only done some of the time, not all of the time.  For example, if you have 10 hits per second, you might set it so the check is done every 100 hits (every 10 seconds).  A simple way to do this is

 

if (rand(1,100) == 1) {
  # do the check
}

Link to comment
Share on other sites

I rellay don't thank that's going to work for my situation. I'm trying to make it so if they close the program or something it still counts down for a time limit to log out. Even when no php script are running, then when it gets to zero it changes a variable in my mySQL server and other people read this and it says he's not logged in.

This requires the use of a database.

Here's a simple breakdown so you can get the gist of what needs to be done:

 

You have a database table, let's call it tbl_sessions for now.  In tbl_sessions, there is a column called last_time.  Every time the user opens a new page on your site, last_time is set to the time() that that page was opened.  If a user doesn't open a page for say...30 minutes, last_time will obviously be set to a time 30 minutes ago.  So, when a user goes to the page to see who is online, your query basically says:

Select everything from tbl_sessions where the column last_time is GREATER THAN time() - 30 minutes.  I'm too lazy to write the SQL for that, although it's extremely simple...oh well, here it is:

$timeFrom = time() - (30 * 60);
$sql = "SELECT * FROM tbl_sessions WHERE last_time > $timeFrom";

That will return only the users who have opened a page in the last 30 seconds.  Now, as for assigning a user to a specific row in a table, that's where the cookies come in.  You have a column in tbl_sessions that is session_id.  In that session_id column, for each user that visits the site, a random 32 character code is generated.  That code is stored in session_id, and is also written to a cookie on the client's computer.  When the client returns to the site, your PHP scripts read the cookie, find that session_id, pull the information from tbl_sessions, and updates the last_time.

So what happens if two people playing are in different time zones, does it save the time to the database locally or globally?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.