Code to calculate number of visitors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luftikus143
    New Member
    • Jan 2007
    • 97

    Code to calculate number of visitors

    Hi there,

    I developed a PHP code calculating the number of visits of a web site based on the more-or-less common 30-minutes approach. But meanwhile, the log-database got so huge, that it takes hours and hours to go through the log. I am sure this is due to an inefficient way to program the code.

    Does anyone have a nice piece of code at hand or know where to look for that algorythm/code?

    Thanks for any advice.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    I usually record the IP of visitors, and match it against a database log table, if it hasn't been counted for an x ammount of time, I add a row.
    Once I've done that I create a session variable which is checked every time a user navigates on my web, so I wont be querying the database every time.

    Example:
    [code=php]
    // Start session
    session_start() ;

    // Connect SQL
    include("myDb.p hp");
    myDb->connect();

    // Get the ammount of visits
    $res = myDb->fetch("SELEC T COUNT(logID) AS 'count' FROM logTb");
    echo "Total visitors: ". $res[0]['count'];

    // Check if you have been counted this session
    if(!isset($_SES SION['HasBeenCounted ']))
    {
    // Fetch the ammount of times
    // this IP has been counted today
    $IP = $_SERVER['REMOTE_ADDRESS '];
    $res = myDb->fetch("SELEC T logID FROM logTbl WHERE IP = '$IP' AND logDate = NOW()");

    // If it has not been counted; add it
    if(count($res) == 0) {
    myDb->query("INSER T INTO logTbl(IP, logDate) VALUES('$IP', NOW())");
    }

    // Set the $_SESSION var
    $_SESSION['HasBeenCounted '] = true;
    }

    [/code]
    Last edited by Atli; Jun 18 '07, 11:33 AM. Reason: Added the example

    Comment

    Working...