- How to create a Session Timer Script -

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Thu Oct 31, 2002 4:06 pm
Location: San Francisco, California

- How to create a Session Timer Script -

Postby Tor » Fri Nov 01, 2002 9:57 am


Session Timer

I use sessions to track the users' actions on my web site, pluss the
members name are listed on my web site on a log status bar. The
problem is that if the users do not log out correctly, their user
name will be showing up on the log status bar after they have closed
their browser. The correct way to log out of my web site is to click
the log out option so the script will remove their name from the
status bar. What I need to figure out is to make a timer which will
delete their active status after, say 30 minutes.

Every page I have use session_start() and I register the user session
variable as $valid_user.

I have a table which I call logstatus. When the user log into the
web site the $valid_user variable value put their username into the
logstatus table, and when the user log out, the $valid_user variable
remove the value from this table.

How can I make a script to
remove the users from this table after 30 minutes when the users do
not log out correctly?

Thanks for any help.


Postby DoppyNL » Fri Nov 01, 2002 10:07 am

You can use javascript to call a page wich will remove them when they leave your site, but not all users got javascript or have it disabled so you can't trust that for 100%.

I think the best way to do it is to store a date+time of the last page a user has requested, you can easily add this to the table "logstatus".
Then you can simply run a delete query before you show wich users are online and remove entry's wich are 30min or older.

Greetz Daan


Postby Jay » Fri Nov 01, 2002 2:05 pm

Just to add to DoppyNL, make a table that stores a user's status, or whether they're online. Every time they access a new page, update the time in the page. Every time the table is queried for display to you, delete any entries older than X minutes, and you'll have your list of current users!

New php-forum User
New php-forum User
Posts: 2
Joined: Thu Oct 31, 2002 4:06 pm
Location: San Francisco, California

Script hint

Postby Tor » Sun Nov 03, 2002 10:01 am

Thanks to both of you, but what I really need is a hint to how my script wil look like. I am not that good at the date and time functions, and my script will not work. How do I declare the 30 minute time limit? Thanks for any help..


Postby Jay » Sun Nov 03, 2002 11:36 am

Store all the times as a timestamp in the table (which will update itself if you don't enter a value specifically). On your query you can delete records which have a timestamp < NOW()-20 minutes. Have a look in the MySQL manual (under chapter 6.4 I think) for the functions you can use.

Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 10 guests