Sessions and Viewers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Sessions and Viewers

Post by Frozenlight777 »

I'm trying to write a little functions that would display the number of concurrent sessions. This site does it with the number of "Guests" but I'm not sure where to begin.

Logically this is how I'm thinking of coding it (pseudo code)

Code: Select all

function foo(){
  $IP = $_SERVER['REMOTE_ADDR'];
  $_SESSION['IP'] = $IP;
  $result = count($_SESSION['IP']);
  return $result;
}
foo();
I thought of using the IP just because of it's unique characteristic, there's got to be a better way to do it.
I know that will only work for each session which would return just 1. Is there a tutorial or something out there someone can share? Thanks guys.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Sessions and Viewers

Post by jaoudestudios »

you have the right idea

only I would not use ip as in theory there should only be 1 ip per user, in actual fact it is not true due to sub networks (local networks, wireless etc).

I would use the php function uniqid(rand(), true).

This will give you a real unique number as it is based on micro unix timestamp.

Also if you want to use count($var) then it needs to be an array....so use $var[] = unique_no as this will keep incrementing the array $var, then you can use count.

This will keep incrementing forever, so you really need to put a time feature on. So that if a user goes visiting around the site their unique id also has a timestamp associated with it which will keep getting updated every time they visit a page. So your count would only count the people who have been active within the last 10mins or so.

Hope that makes sense.
Post Reply