By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

PHP Users Online Script

eragon
100+
P: 431
Hello good people of TSDN! I have a little script here that need help with... I want a script that tells me how many people are viewing a page or a directory at a time, EG: How many users are on index.php or how many users are in the /forums directory. I have a hit counter script that i will post here for anybody wanting one, and i think it works great, i just need a counter that goes down when a user leaves directory. I would also like a counter that counts how many people have the session string ['userid'] in their browser, which is m sites way of telling if your logged in or not. Can somebody please help me on this?

The counter code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $count_my_page = ("hitcounter.txt");
  3. $hits = file($count_my_page);
  4. $hits[0] ++;
  5. $fp = fopen($count_my_page , "w");
  6. fputs($fp , "$hits[0]");
  7. fclose($fp);
  8. echo $hits[0];
  9. ?>
PS: I wish not to use MySQL, i don really like that database type... Long ago i made a deal with MySQL, i said "Be nice and work for me, and i will allow you to exist..." MySQL got mad and said "I am never nice to amateurs such as yourself! Muahaha" So i took my database and shot it square between the Y and the Q.
Jul 14 '07 #1
Share this Question
Share on Google+
10 Replies


Atli
Expert 5K+
P: 5,058
Hmm, I do not think it is possible to view session data from other users, so tracking how many users are logged in that way won't work.

You can never catch when a user leaves a page. That is; a user can close his/her browser/tab and you will never know about it.
So the only way to track what a user is viewing is to have the page currenty open write that it has been opened somwhere, and then invalidate that log entry when a preset period of time has passed.

This would be fairly simple using a database, like say MySQL.
You would simply create a table that stored the current location of each user, and then select those records created/updated whitin a set ammount of time.

This way you can get a pretty accurate estimate of how many users are logged in and what each user is up to.

If you do not want to use a database you will have to create your own system of storing the data you need, which in my experience is much more trouble than just using the damn database, even if you have no or litte experience with it.
Jul 15 '07 #2

eragon
100+
P: 431
Then i shall use SQL once more... i looked at some flat file scripts, and if they werend encoded they were really huge! Now, if iuse SQL, can i get it to update the information WITHOUT reloading the page? EG: I can just sit back and watch the numbers change?
Jul 15 '07 #3

Atli
Expert 5K+
P: 5,058
Then i shall use SQL once more... i looked at some flat file scripts, and if they werend encoded they were really huge! Now, if iuse SQL, can i get it to update the information WITHOUT reloading the page? EG: I can just sit back and watch the numbers change?
Well, any code received by your browser is just 'static' output from the server, so the short answer is no.

HOWEVER, if you want the long answer, then it would be 'AJAX'. Which is basicly Javascript that you can use to periodicly send a request to the server, asking for updates which you can then use to update the 'static' output. (which btw is the reason for the quotes arround the static part)

So, yes it is possible, but in my honest opinion, It would be much simpler to just press the refresh button every now and then.

P.S.
This really has nothing to do with SQL, it would be the same whatever method you would use to track your users.
Jul 15 '07 #4

eragon
100+
P: 431
ones just easier, right? and btw, if anybody has codes that they are willing to share, AJAX, or just PHP flat, or PHP MySQL, please share! And just a note, i am on a free server, so i have limitations with MySQL. Thats half the reason i dont want to use it, 110mb.com might take away the databases to free users, so i have been avoiding the dolphin database... and my pinguin operating system isnt helping either...
Jul 15 '07 #5

pbmods
Expert 5K+
P: 5,821
Heya, eragon.

The way the major forum sites do it is they store the page each User accessed, as well as a timestamp, in the database. No User may have more than one entry in this table, so every time a User goes to a different page, his data changes for that table. Note also that refreshing the page also 'refreshes' the visit timestamp.

Then the server looks up how many accesses to a given page there have been in the last 5 (or 10 or 3000) minutes.

Since you have no way of knowing when Users leave your site, this makes a good compromise, especially if your Users are opening up your site and then walking away from their computers for extended periods of time, for example.

In terms of the database, never let anything get in your way because it's hard or intimidating. Simply reinstall the software or recreate the database and try again. We've all been there, and that's why sites like this exist.

If you ever have any problems, post on the MySQL forum anytime.
Jul 15 '07 #6

kvijayhari
P: 25
i've found a free users online script at hscripts.com and it's a simple script which tracks the users based on the ip and the pages they are visiting.
find the script at User online script

I think it would satisfy your need.
Jul 16 '07 #7

eragon
100+
P: 431
Ok, ok, thats very nice of you. ill install this script and see how it works, but as i said, 110mb is going to take away the free databases soon, but for now, i still have it.
Jul 16 '07 #8

eragon
100+
P: 431
How come google didnt tell me of this before?!?! This script is awesome in the sense that it is not encrypted :p im gonna use this script and i am going to hav to finish my darn site.

and by the way, how come smilies arent working?? :) :(

(_)=3 <Chicken Leg
Jul 16 '07 #9

pbmods
Expert 5K+
P: 5,821
Heya, eragon.

and by the way, how come smilies arent working?? :) :(

(_)=3 <Chicken Leg
Smilies are turned off in technical forums to prevent smilies from showing up in source code.

Yum. Chicken.
Jul 16 '07 #10

eragon
100+
P: 431
that makes sense.....

some body new to php writes:

How would i make an echo, like this:

echo:(.:.:.:)
and how about defining a string:$string=('::Fish::');

... wait a minit, how come theres pics in my source code?
Jul 16 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.