Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<b>Members Online script</b> 1

Status
Not open for further replies.

4x4uk

Technical User
Apr 30, 2002
381
GB
I currently have a members only area on my website which uses php session to protect the pages from non members. The username and passwords are stored in text file (password md5 encoded) if the username and password are valid the following code is executed

Code:
//validation of member username & password
session_start(); 
$_SESSION['members'] = $_POST['uname'];
Header("location:../Members/welcome.php");

I would like to display the number of members online in both the protected and unprotected areas. Does anyone no of a simple way of acheiving this?
 
There is no good way to do with with a web interface. Not if you want the number you present to have any bearing on reality. HTTP is a stateless connectionless protocol.

If you track each user as he logs in, you might be able to present that number. In your user database, create a field which delineates whether a user is logged in or not. Then when you need to present the number, count the number of users whose current status is "logged in".

There is not much you can do, though, to keep that number accurate. You can't stop a user from logging in, then shutting down his browser without logging out.
 
Thanks that's what I thought would be the answer. I think I'll just stick to logging the date and time a user 'logs on' in a text file as I do at the moment
 
Sorry, I'm not sure that what I'm going to say will be useful, but wouldn't be enough to open a session for each user and then, to count all the session files that are inside the folder tmp (where sessions are stored) ?

Never done that. Tell me if the idea is stupid.


My Work...
...and More...
 
Sessions may last for longer than a user is actually in the web site. The number of session files on the filesystem (or number of records in a table, if you are using SQL-based session handling) will almost certainly be larger than the number of active logged in users.

Also, session cookies are normally set to exist only during the current session. If I log into the site, a session record is created and the session cookie is set in my browser. If I shut down my browser and return to the site and log in before my original session record is due to expire, then I will be assigned a new session, so there will be two records for my one login.

It's just the nature of HTTP. FTP, for example, is a connected protocol. I can run ftpwho on my server and see how many users are logged in at any given instant because a login exists only while the user is connected.
 
That was exactly the problem I was hitting when trying to get a reliable count of the number of sessions running hence the original question. I decided that I would rather not display inaccurate information on the site. What I am going to do instead is allow Logged in members to view the log file which is updated by the script when a member logs in
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top