Session problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex7900
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 4:42 pm

Session problems

Post by alex7900 »

im new, so sorry if i violated some a rule or 2.. :(

im making a website which is like a forum
everything is working great, the accounts,topics,comments,etc but the only problem i have is that im trying to implement something that tells you whos online or not.
This is what im doing:

1 database to store all of the online accounts.

when a session starts(user logs in) i add their name to the "online" list.

when the session ends(user logs out) i remove their name from the "online" list

it works, but the only problem is that idk how to check if the user navigates away from the site, or closes the browser so if they do that, they might stay online, or logg in twice >.<
jeancaffou
Forum Newbie
Posts: 8
Joined: Wed Nov 26, 2008 3:57 pm

Re: Session problems

Post by jeancaffou »

There is no way of knowing when the user session ends. Try making something like: "Users active in the last 5 minutes".
alex7900
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 4:42 pm

Re: Session problems

Post by alex7900 »

im still not really sure how sessions work, do they end automatically after a time period?
jeancaffou
Forum Newbie
Posts: 8
Joined: Wed Nov 26, 2008 3:57 pm

Re: Session problems

Post by jeancaffou »

session works like this:

PHP gives user a cookie with PHP_SESSID key

with that key, php can access it's own "cookies" (they're not really cookies) - variables that can be reached only with that key

that cookie expires when the user closes his browser

when that cookie doesn't exist anymore, the session expires, quits, resets, whatever you programmed.


session is just a nice way of storing temporary variables.
http://si2.php.net/manual/en/intro.session.php give it a read
pinoypride
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 7:46 pm

Re: Session problems

Post by pinoypride »

One solution I can think of right now is to use time().

Syntax:
$timestamp = time();
$timeout = $timestamp - 900;

This cannot detect if a user closes the browser. However, if you are looking for some ways to detect if a user is actively browsing your site, this would definitely help. How does it work?

Like for an example, John Doe visited this specific link from your site http://website.com/chat.php.

In your php coding, you will have to append a new timestamp record in your database together with user's IP address.

MySQL Table record should be like this: (John Doe IP Address -> 00.000.000)

Tbl name: active_users
IP ADDRESS - TIMESTAMP
00.000.000 - 0903100
00.000.000 - 0904100
00.000.000 - 0906100

Everytime "chat.php" is loaded either by John Doe or by someone else, a function (let's call this removeInactive function ) will get the latest timestamp. A conditional statement will automatically detect and remove expired timestamp from your database. This way, you can keep track on your record if John Doe is still active in your site disregarding the fact the he closes the browser or simply left w/out logging out.

removeInactive function will do the following task everytime you call it in any particular page:

- if John Doe's timestamp is not yet expired, simply append new timestamp record
- detect and remove expired timestamp from active_users table
- display the latest record of active_users table

Here are some codes you might want to know:

Code: Select all

 
 
# Fetch Time
# $timeout value is based on seconds, minutes and hours.
 
   $timestamp = time();
   $timeout = $timestamp - 900;
 
# To insert new timestamp
   $insert = mysql_query("INSERT INTO active_users (timestamp, ip) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."')") or die("Error!");
 
# To remove expired timestamp
  $delete = mysql_query("DELETE FROM active_users WHERE timestamp<$timeout") or die("Error!");
 
 

Hope this helps.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Session problems

Post by novice4eva »

There is javascript to check if the client has closed his window or not. Then you can use ajax to do the rest of your task. viewtopic.php?f=1&t=90176, there is a link given by mmj, i could have given you just the link but the post can be a bit help to you too. I would like to disagree with jeancaffou, sessions do not expire when the user closes the browser(haven't seen that happen in my setup, i'll play around with the session setting and let you know), we can do a simple verification to check that. Sessions do expire automatically after certain predefined period specified by session.gc_maxlifetime though.

EDIT: Yes session expired on browser close/refresh when the session.use_cookies was disabled(0) because i didn't send the session id as get parameter but not when they were enabled which is set by default.
Post Reply