|
 |
|
Dev Articles Community Forums
> Programming
> Programming Tools
|
Article Discussion: The Quickest Way To Count Users Online With PHP
Discuss Article Discussion: The Quickest Way To Count Users Online With PHP in the Programming Tools forum on Dev Articles. Article Discussion: The Quickest Way To Count Users Online With PHP Programming Tools forum for discussing any topics that don't fit into the programming categories above. Find information on tools and utilities to increase any programmer's productivity.
|
|
 |
|
|
|

Dev Articles Community Forums Sponsor:
|
|

October 4th, 2002, 08:40 PM
|
|
|
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 588
Time spent in forums: < 1 sec
Reputation Power: 17
|
|
Article Discussion: The Quickest Way To Count Users Online With PHP
|

October 4th, 2002, 10:06 PM
|
Registered User
|
|
Join Date: Oct 2002
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I would suggest using if(is_file($Fil)) instead of the method in the article becuase the way shown, I have add some errors. Over all the I thought the article was great!
|

October 6th, 2002, 11:13 PM
|
Registered User
|
|
Join Date: Jun 2002
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
got Error
greeting,
got some errors
my getOnlineUsers.php
PHP Code:
<?
session_save_path("E:\\homepage\\shapik\\vthree.0\\sessions");
/* Start the session */
session_start();
/* Define how long the maximum amount of time the session can be inactive. */
define("MAX_IDLE_TIME", 3);
function getOnlineUsers(){
if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
// Comment the 'if(...){' and '}' lines if you get a significant amount of traffic
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
$count++;
}
}
closedir($directory_handle);
return $count;
} else {
return false;
}
}
echo 'Number of online users: ' . getOnlineUsers() . '<br />';
?>
my index.php
PHP Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include('getOnlineUsers.php');
echo 'There are ' . getOnlineUsers() . ' user(s) currently browsing MyDomain.com';
?>
</body>
</html>
Last edited by sicksand : October 6th, 2002 at 11:27 PM.
|

October 6th, 2002, 11:17 PM
|
Frank The Tank!
|
|
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240
Time spent in forums: < 1 sec
Reputation Power: 18
|
|
What error messages are you receiving?
|

October 7th, 2002, 12:00 AM
|
Registered User
|
|
Join Date: Jun 2002
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Parse error: parse error, unexpected T_ELSE in e:\homepage\shapik\vthree.0\getOnlineUsers.php on line 24
Fatal error: Call to undefined function: getonlineusers() in e:\homepage\shapik\vthree.0\index.php on line 12
|

October 7th, 2002, 11:01 PM
|
Frank The Tank!
|
|
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240
Time spent in forums: < 1 sec
Reputation Power: 18
|
|
You're missing a closing brace "}" in your getOnlineUsers() function. I would suggest indenting your code, this way you can find errors like this more easily. It provides easier readability.
Try that.
|

October 11th, 2002, 08:44 PM
|
Registered User
|
|
Join Date: Oct 2002
Posts: 4
Time spent in forums: 2 m 43 sec
Reputation Power: 0
|
|
hey everbody! i'm new here  and i was testing this script out and aready solved the missing bracket problem, but now i run into a new problem and where it supposed to say "users online: "
it says
Warning: open_basedir restriction in effect. File is in wrong directory in /mnt/host-users/cumnut/index.php on line 10
users online:
any help?
|

October 11th, 2002, 09:08 PM
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
this means that your having problems with setting the session data i think?? does your server allow sessions to be used?
|

October 11th, 2002, 09:23 PM
|
Registered User
|
|
Join Date: Oct 2002
Posts: 4
Time spent in forums: 2 m 43 sec
Reputation Power: 0
|
|
how do i find out if my server allows sessions?
i tried using phpinfo()
and said phpinfo() was dispabled for security reasons...
|

October 11th, 2002, 09:51 PM
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
sounds like its running in safe mode maybe?
|

October 11th, 2002, 09:54 PM
|
Registered User
|
|
Join Date: Oct 2002
Posts: 4
Time spent in forums: 2 m 43 sec
Reputation Power: 0
|
|
is their any way i can change that?
|

October 11th, 2002, 10:20 PM
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
well that depends if you have administrator rights to the actual web server? If your using a hosted web server, your got bugger all chance.
However, things will lookup if your running your own server
look for "safe_mode" (without the quotes) in your php.ini file, and make sure that its set to "Off" (again without the quotes)
save it, and restart the server.
|

October 12th, 2002, 03:07 PM
|
Registered User
|
|
Join Date: Oct 2002
Posts: 4
Time spent in forums: 2 m 43 sec
Reputation Power: 0
|
|
 i'm hosted!! well, worth a try anywayz....
maybe i'll change the host...
|

November 3rd, 2002, 06:11 PM
|
Registered User
|
|
Join Date: Nov 2002
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Hi, first of all I'm sorry for my poor English.
The method described in the article is as great as simple but I would never suggest anybody to save sessions in files in a very busy site. In this case, as far as I know, it would be better saving sessions in dbm files or in a database.
Have you ever tried, by any chance, to use your method on a dbm files based session system and count the keys instead of files?
|

February 20th, 2003, 08:19 AM
|
Registered User
|
|
Join Date: Feb 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Hey, dont know if anyone else has run into this problem, but fileatime() does not return the access TIME of the files on a Windows platform, only the date.
Not sure if there is a work around for this.
Any ideas?
I have already edited the script some what to only count session files that have certain values within them, so as to only count session files of registered members
goes like this :
PHP Code:
session_start();
define("MAX_IDLE_TIME", 3);
function getOnlineUsers(){
if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
$readfile = session_save_path() . $file;
echo fileatime($readfile);
$fr = file_get_contents($readfile);
if(strstr("$fr", "s_username")) {
$count++;
} } }
closedir($directory_handle);
return $count;
} else {
return false;
}
}
echo 'Number of registered users online: ' . getOnlineUsers() . '<br />';
Wonder if adding a "last accessed date" to the session everytime it is accessed then pulling it out with any other values as above would solve this problem, or if it would slow everything down.
Any help welcome.
Thanks 
|

February 20th, 2003, 08:39 AM
|
Registered User
|
|
Join Date: Feb 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
lol..
Nevermind, created a work around.
Just incase anyone else needs it...
everytime you start the session:
PHP Code:
session_start();
$_SESSION["lastaccess"] = time();
then the script itself:
PHP Code:
session_start();
define("MAX_IDLE_TIME", 3);
function getOnlineUsers(){
if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
$readfile = session_save_path() . $file;
$fr = file_get_contents($readfile);
list($lastaccessstr) = explode(";",$fr);
list($lastr,$lastaccess) = explode(":",$lastaccessstr);
if(time()- $lastaccess < MAX_IDLE_TIME * 60) {
if(strstr("$fr", "s_username")) {
$count++;
} } } }
closedir($directory_handle);
return $count;
} else {
return false;
}
}
echo 'Number of registered users online: ' . getOnlineUsers() . '<br />';
I know its not commented, but i'm sure you can work it out yourself, besides, I doubt anyone is going to read this 
|

March 23rd, 2003, 05:38 PM
|
Registered User
|
|
Join Date: Mar 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
hi everybody!
Mr author you have written a really usefull article but it has so many bugs!
plz correct this as soon as possible ...
thanx again
|

March 23rd, 2003, 05:39 PM
|
Registered User
|
|
Join Date: Mar 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
hi everybody!
Mr author you have eritten a really usefull article but it has so many bugs!
plz correct this as soon as possible ...
thanx again
|

April 13th, 2003, 08:04 PM
|
Registered User
|
|
Join Date: Apr 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Does anybody know what the problem may be, if my $file only contains a ”.”
|

July 25th, 2003, 06:46 AM
|
Registered User
|
|
Join Date: Jul 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
i'm having problems as well, but i dont know if it is really the author's code that
my counter always shows too many users online.
whenever i switch to another page the counter adds about 20 hits or so. why? isnt it the same session?
is there an increasement whenever i make session_start(); ???
i'm new to sessions , pardon!
and i want to show how many users are online on the whole website. nobody but me can be online cause it is a restricted testarea, but it shows about 100 users.
thanks for any hint!!!!!
sascha
|

March 10th, 2004, 08:16 PM
|
Registered User
|
|
Join Date: Mar 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I used this code and i still get 0 users online, i checked my error and nothing, but i have to change my saves seesion path to /tmp/ casue i got an error of /tmpsess.2345678uygfcfgh file not found, so when i changed my path i still get 0 users on line
|

April 28th, 2004, 10:23 PM
|
Registered User
|
|
Join Date: Apr 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I don't like leaving negative feedback, but I've wasted hours on this bit of code and I still get a user count of zero, which I know to be wrong. I really think the author should have made sure the correct number of brackets were on the page and it's been commented on enough in this discussion, which is where I eventually worked out where it should go.
Why ask for a discussion on the article if the author doesn't read it?
|

February 11th, 2006, 03:46 PM
|
Registered User
|
|
Join Date: Feb 2006
Posts: 2
Time spent in forums: 39 m 14 sec
Reputation Power: 0
|
|
Quote:
Originally Posted by FrankieShakes
You're missing a closing brace "}" in your getOnlineUsers() function. I would suggest indenting your code, this way you can find errors like this more easily.
|
Actually, it was the author how is missing a closing brace.
While in theory this was a good article, in practice, I am quite disappointed that it wasn't tested enough to find a missing brace!
I had to give this one star. If you are writing an article, at least test it, please.
|

February 12th, 2006, 05:08 PM
|
Registered User
|
|
Join Date: Feb 2006
Posts: 2
Time spent in forums: 39 m 14 sec
Reputation Power: 0
|
|
Quote:
Originally Posted by FrankieShakes
What error messages are you receiving?
|
Here is the script with the { problem fixed.
Adding this line at the end:
echoing session_save_path(); out shows it is in the proper directory.
The solution for Linux is to change this line:
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
to this:
if(time()- fileatime(session_save_path() . '/' . $file) < MAX_IDLE_TIME * 60) {
<?php
/* Start the session */
session_start();
/* Define how long the maximum amount of time the session can be inactive. */
define("MAX_IDLE_TIME", 3);
function getOnlineUsers(){
if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
// Comment the 'if(...){' and '}' lines if you get a significant amount of traffic
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
$count++;
}
}
}
closedir($directory_handle);
return $count;
} else {
return false;
}
}
?>
|
Developer Shed Advertisers and Affiliates
Thread Tools |
Search this Thread |
|
|
Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|