SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to detect number of visitors on the entire site?

    I was wondering if it was possible to place some php code on each and every page of my content on my site to detect the number of visitors on the site at any one moment just like what PHPNuke is able to do.

    Eg.

    Page 1 --> *php code -> 2 visitors
    Page 2 --> *php code -> 6 visitors
    Page 3 --> *php code -> 4 visitors

    Secret url (e.g http://www.mydomain.com/visitors.php) -> 12 visitors

    Any way to do that?

  2. #2
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not difficult but it's not an easy two-line explanation.

    You should create a sessions heap table. At the top of every page of your site you include (sessions.php).

    In that sessions.php you first query the sessions table to see if this IP address has a current session. If not, you insert into the session table. If it does, you update the table with their current location.


    ... here's a question: how do you delete old sessions? I've never looked to see how VB does it. It would be too much overhead to clean the session table with each update, so when do you go in and expire the old sessions? Cron job?
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  3. #3
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have actually done this before and it takes a bit of genius to actually come up with it, though it is rather simple.

    What you do is first gather the user's IP address and shove it into a variable. Then make a variable with a number for your expire time in seconds as your timeout, such as 300 per say. Then make a table called sessions with an ID,IP, and last_time area for the last time it was updated.

    Then there are 3 main queries you need to do, INSERT, UPDATE, and DELETE (which is the main garbage collection query)

    what you have it do first is check if the IP is IN the database, and if it isint, use an INSERT query to load it in and makesure you use time() to load the unix timestamp. Then put an ELSE for is the users IP does exist, then do an UPDATE and set the users session entry to the new time()

    After the IF statement, comes the garbage collection routine, which makes it all automatic. The DELETE query should do the following:

    check if the user's time has expired past the expire time (this is where the variable comes in) The math for this shoudl be done in this type of fashion: (time() > (last_time + $expire))

    Thats just guidelines, its a fundamental, but it works. The way php functions right now forces you to do automated site functions via the user calling a main script file and doing the raw update routines that way.

  4. #4
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or just parse the server log file... keep count of uniqe IPs until you get to N minutes... no need for a SQL database. If that (or any method) takes too much CPU time, every minute just run via cron to a text file and insert into document using an include.

    This should be fairly trivial to implement (and doesn't need SQL!)

    Owen

  5. #5
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...but the database thing is easier speaking in relative terms.
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  6. #6
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not a massive fan of sessions, so I've written my own implementation of them using PHP and mySQL. This allows me to do lots of funky things, including counting the number of users on site at once.

    Basically when a user first hits a page you detect that they are a new user because they don't have a cookie set. Create a nice big random string and send it to them as a cookie which expires in say 60 minutes - then save that string in a database table along with a timestamp of the current time.

    Each subsequent visit the user makes to a page in your site, have the script detect their cookie and look them up in the database table. If the cookie is there and hasn't expired yet (the script can see if it's expired by adding the expiration time in seconds to the time stamp saved in the database) don't do anything - if they don't have a cookie set one and add it to the database.

    To see how many users are on site at once, all a script has to do is count the number of active sessions in the database table (active sessions can be seen by checking for sessions with a start timestamp occuring in the last 60 minutes or whatever).

    The downside to this is that if the user has cookies turned off it won't work, so it's a advisable to track their IP as well if you want to use this seriously. I use this system to allow users to log in with their username and password - the session table stores their user ID allowing me to securely check a user's permissions on every page they visit.

  7. #7
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See http://www.stonehenge.com/merlyn/Web...ues/col04.html for an (overly complex) Perl script to print out the last N visitors... Similar to what you want. it shows the IPS of the last visitors... Just for some ideas on how to do it.

    BTW: not everyone writes Perl so cryptic... I think he is trying to demonstrate more advanced techniques w/ this simple example.

    Owen


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •