Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2002, 07:40 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 588 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Article Discussion: The Quickest Way To Count Users Online With PHP

The Quickest Way To Count Users Online With PHP If you have any comments or questions about this article then please post them here.

You can read the article here .

Reply With Quote
  #2  
Old October 4th, 2002, 09:06 PM
Adman Adman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 5 Adman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #3  
Old October 6th, 2002, 10:13 PM
sicksand sicksand is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 7 sicksand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 opendirsession_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 10:27 PM.

Reply With Quote
  #4  
Old October 6th, 2002, 10:17 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
What error messages are you receiving?
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #5  
Old October 6th, 2002, 11:00 PM
sicksand sicksand is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 7 sicksand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old October 7th, 2002, 10:01 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
Send a message via ICQ to FrankieShakes Send a message via MSN to 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. It provides easier readability.

Try that.

Reply With Quote
  #7  
Old October 11th, 2002, 07:44 PM
tab tab is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 4 tab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #8  
Old October 11th, 2002, 08:08 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
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?

Reply With Quote
  #9  
Old October 11th, 2002, 08:23 PM
tab tab is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 4 tab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #10  
Old October 11th, 2002, 08:51 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
sounds like its running in safe mode maybe?

Reply With Quote
  #11  
Old October 11th, 2002, 08:54 PM
tab tab is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 4 tab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 43 sec
Reputation Power: 0
is their any way i can change that?

Reply With Quote
  #12  
Old October 11th, 2002, 09:20 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
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.

Reply With Quote
  #13  
Old October 12th, 2002, 02:07 PM
tab tab is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 4 tab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #14  
Old November 3rd, 2002, 05:11 PM
Fabio Heller Fabio Heller is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 1 Fabio Heller User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #15  
Old February 20th, 2003, 07:19 AM
Mezza Mezza is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 Mezza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 opendirsession_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

Reply With Quote
  #16  
Old February 20th, 2003, 07:39 AM
Mezza Mezza is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 Mezza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 opendirsession_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

Reply With Quote
  #17  
Old March 23rd, 2003, 04:38 PM
joukar joukar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 1 joukar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy

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

Reply With Quote
  #18  
Old March 23rd, 2003, 04:39 PM
joukar joukar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 1 joukar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy

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

Reply With Quote
  #19  
Old April 13th, 2003, 07:04 PM
Charlotte Charlotte is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 1 Charlotte User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Does anybody know what the problem may be, if my $file only contains a .

Reply With Quote
  #20  
Old July 25th, 2003, 05:46 AM
sascha sascha is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 1 sascha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #21  
Old March 10th, 2004, 07:16 PM
andrewld andrewld is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 andrewld User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #22  
Old April 28th, 2004, 09:23 PM
lakrasia lakrasia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 lakrasia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #23  
Old February 11th, 2006, 02:46 PM
CentaurAtlas CentaurAtlas is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 CentaurAtlas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #24  
Old February 12th, 2006, 04:08 PM
CentaurAtlas CentaurAtlas is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 CentaurAtlas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

}
?>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > Article Discussion: The Quickest Way To Count Users Online With PHP


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap