Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
Rightie ho, i want to make a script with php that will tell me on one page who is accessing another page.

Variables are $username, and $page. Every page on my site is have a $page = "page" and every username is well $username as far as the page is concerned.
 
I would think that you could have a script that updates a table in a database as the user moves around. On each page access it updates the table saying where they are. Then on your page it simply reads that table and outputs it. I don't have any code, but doesn't sound horribly hard to do, but might be a pain to add code to each page to update the table.
 
I would think that you could have a script that updates a table in a database as the user moves around. On each page access it updates the table saying where they are. Then on your page it simply reads that table and outputs it. I don't have any code, but doesn't sound horribly hard to do, but might be a pain to add code to each page to update the table.

Im more looking to try to make a solution that doesn't involve the database, perhaps using session variables.
 
Not to totally discurrage you from this, but I recall reading something a while back on a forum dedicated to PHP development where someone wanted to do this.

The end result was much more complicated than he originally thought.

When I saw this thread I tried to figure out how to do it in my mind, however the only easy way I could think of would be to handel it in a database. If you already had a user table setup it could be very very simple...

If you really want to do this your best bet would be to go to a forum dedicated to PHP or another server-side technology. :)
 
Not to totally discurrage you from this, but I recall reading something a while back on a forum dedicated to PHP development where someone wanted to do this.

The end result was much more complicated than he originally thought.

When I saw this thread I tried to figure out how to do it in my mind, however the only easy way I could think of would be to handel it in a database. If you already had a user table setup it could be very very simple...

If you really want to do this your best bet would be to go to a forum dedicated to PHP or another server-side technology. :)
Aye i am on phpbuilder just now, worked out that a database is the best way but i don't know how to set it up so that it can send a request to the database when the user navigates away from the page.
 
Aye i am on phpbuilder just now, worked out that a database is the best way but i don't know how to set it up so that it can send a request to the database when the user navigates away from the page.

The easiest way would not be when they navigate away from a page, but when they navigate to a page. When they navigate to the page just query the DB

Code:
UPDATE $table (PAGE) VALUES ("$page") WHERE USER=$user

something along those lines, :)
 
Aye i am on phpbuilder just now, worked out that a database is the best way but i don't know how to set it up so that it can send a request to the database when the user navigates away from the page.

Well there's always JavaScript that can detect onunload, but not likely a great solution. I would say just set an expiration, say 20 minutes. Then when you update a user for a page you can also do a check for expired 'sessions.' I do something very similar for a feedback form validation when checking how old a session is.

If you didn't want to use a database you could use a flat file (plain text), with a user per line that is online,
Code:
user#timestamp#page
It would take a bit of parsing, but doable.
 
The problem comes were its only for when the user is in the chat room and change the status the second they leave the chat room page and go onto a different page.
This is kept in my whos online box that shows all online users and is updated every 5 seconds.
The plan is to have a little bubble next to the users name if there in the chat room.
 
The problem comes were its only for when the user is in the chat room and change the status the second they leave the chat room page and go onto a different page.
This is kept in my whos online box that shows all online users and is updated every 5 seconds.
The plan is to have a little bubble next to the users name if there in the chat room.

Still using a database it isnt a problem.

on the page that is listing peoples location.
put
PHP:
if ($page =='chatroom') {
echo <img src='chatbubble.jpg'>; }
echo $User. '  ' . $page;

obviously it would give syntax errors but the idea is there. if the page is the chatroom, itll put a bubble by the name. otherwise itll just display the name. the page refreshing is handled near the heading.
 
I wrote a fairly simple forum from scratch with PHP. For "user's online," I just maintain a separate MySQL "MEMORY" table with all the sessions and their last activity (unix epoch is convenient). You could use some AJAX if you want to provide a more "real-time" chatroom, but that adds a bit more complexity.


I then do something along the lines of:

$tooOld = time() + 300;
$sessionID = session_id();
[DELETE FROM online_users WHERE last_activity<$tooOld AND session_id!=$sessionID]
[SELECT * FROM online_users]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.