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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine how many users are visiting a site -now 1

Status
Not open for further replies.

dkdude

Programmer
Jun 16, 2003
849
DK
A client of mine would like to be able to see the number of users visiting her site - real time.

I know that I can examine the server access log to pull the information, but the provider won't give me access to it.

Is that information available through any Apache server vars? Do I need to use sessions?


Any thoughts?
 
You can count the sessions still alive. It's not perfect, but it's reasonable.

'ls -1 /tmp/sess* | wc -l'

Or if you're using sessions from SQL, it's a simple call away...

Now, if you're NOT using sessions, then you'd need to build something into the cookies to post a value that has, say, a 5 minute time-to-live that your server keeps. If that cookie doesn't come by to refresh the "timer" then you decrement your live user count.

D.E.R. Management - IT Project Management Consulting
 
Thanks thedaver

Neat and simple solution, just to count the session files. I will try to see if her provider will let me access the session dir. I know I can use your idea elsewhere, so have a star :)

Just to be sure I got sessions right - the won't work if the client have turned off cookies, right?
 
PHP sessions are adaptive (unless you over-ride).
Sessions are either carried through cookies or as a getstring item on the URL.

Irrespective of the client side, your server should only maintain them in one place, unless your code is over-riding the Apache (or web server)'s default with PHP specific isntructions to write to a RDBMS or RAM.

(Your star didn't stick, BTW, but thanks!)

D.E.R. Management - IT Project Management Consulting
 
... sorry about the star. This time I did actually click on the link ;-)

I will give it a go - it's a nice and speedy solution and works fine on my development server. Sweet!
 
the "now'ness" of the session directory will depend on you garbage collection and session lifetime parameters in your php.ini file.

i mostly prefer to have sessions last quite a while (at least a day or so) as I tend to use them both for logging and for user navigation/preference management (I time out login connections separately).

so for active users I tend to use an alternative which is to create a small table with fields sessionID and lastseen. sessionID must be a primary key

then in each page run the following
Code:
session_start();
define ("SESSIONTABLE", ""); //session table
$now = time();
$active = 10; //definition of active in minutes
//connect to db

flush_nonActive()
touchSession();
$num_active_users = getActiveUsers();

//these could be held in a library file
function flush_nonActive(){
  //deletes records that have not been 'touched' in last x minutes
  mysql_query("delete from ".SESSIONTABLE." where lastseen < " $now-$active);
}

function touchSession() {
 //touches a session to record last activity time
 mysql_query("replace into ".SESSIONTABLE." set sessionID = '".session_id()."', lastseen=$now");
}
function getActiveUsers() {
 //simple record count
 return mysql_result(mysql_query("select count(*) from ".SESSIONTABLE), 0, 0);
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top