Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    So, I decided to start my own counter project since.. well, I couldn't find any that really suited what I wanted.

    What I need help with:
    -Optimization! I know jack about it need to know how to make this the fastest it can b
    e
    -Perhaps people to write more stuff to get browsers/oses out of the user-agent string... I've noticed not many servers have the browscap set right, so get_browser is out. Totally if you wanna

    -People to test it out... even if you have it silent (comment out the makecounter function call) I'd like to see how my graphs work and if it works right.

    -Pointing out any errors/loopholes


    Keep in mind this is my first alpha version.. it doens't include half the options I'll eventually code in, only the basics right now.

    Advantages mine will have to x-counter:
    -One script. No messing aroudn between files, just one script.
    -Easy admin interface to easily block out ips and change database options.
    -Easy install script - no more messsing around in other people's php code, just fill in a few boxes and it's on its way.
    -Extensive statistics Breakdown by months, pages etc.

    Here's my code: feel free to download attatched file if you want to test it out.

    PHP Code:
    <?
    // WCounter - Free PHP counter system.
    // Version 0.1A
    // 5-26-03
    // Copyright 2003 Warpspire - Kyle Neath

    /****************************************SETTINGS****************************************************/
    $TABLEPREFIX "wcounter";  // The prefix to all counter tables
    $MAXTIME 300// The amount of time between a "hit" in seconds.  (i.e. 300 = 5 minutes)
    $TRACKPAGES TRUE//Track individual pages
    $DBUSER ""// Database User
    $DBPASS "";  // Database Password
    $DBDOMAIN "localhost"// Database Domain i.e. localhost
    $DBNAME ""// Database Name
    $IGNOREIP = array(); //IP's to ignore, Example: array("111.111.111.111", "222.222.222.222");
    // The following is the output function.
    function makecounter($totalh$pageh){
        echo 
    "<p align='right' style='font-size:10pt;'>$totalh total hits | $pageh hits to this page</p>";
    }
    /**************************************END SETTINGS***************************************************/
    // There is no need to edit anything below this line in order for the counter to function normally

    // Connect to database
    $db mysql_connect($DBDOMAIN$DBUSER$DBPASS);
    mysql_select_db($DBNAME$db);

    $action $HTTP_GET_VARS['action'];
    switch (
    $action){
        case 
    "stats":
        
    // Make general statistics for site
        
        // Get total hits
        
    $sql "SELECT access_date FROM " $TABLEPREFIX "_data WHERE 1";
        
    $result mysql_query($sql);
        
    $totalhits mysql_num_rows($result);
        
        
    // Get hits today
        
    $sql "SELECT access_date FROM " $TABLEPREFIX "_data WHERE YEARWEEK(access_date) = YEARWEEK(NOW()) AND DAYOFWEEK(access_date) = DAYOFWEEK(NOW())";
        
    $result mysql_query($sql);
        
    $todayhits mysql_num_rows($result);
        
        
    // Produce Hourly Graph
        
    for($i 0$i<24$i++){
            
    $sql "SELECT COUNT(access_date) FROM " $TABLEPREFIX "_data WHERE HOUR(access_date) = $i";
            
    $result mysql_query($sql);
            list(
    $hourhits[$i]) = mysql_fetch_row($result);
        }
        
    $maxval max($hourhits);
        
    $barsize 99;
        for(
    $i 0$i<24$i++){
            
    $hourhits[$i] = $hourhits[$i]*$barsize/$maxval 1;
        }
        
        
    // Produce Daily Graph
        
    for($i 1$i<8$i++){
            
    $sql "SELECT COUNT(access_date) FROM " $TABLEPREFIX "_data WHERE DAYOFWEEK(access_date) = $i";
            
    $result mysql_query($sql);
            list(
    $dayhits[$i]) = mysql_fetch_row($result); 
        }
        
    $maxval max($dayhits);
        for(
    $i 1$i<8$i++){
            
    $dayhits[$i] = $dayhits[$i]*$barsize/$maxval 1;
        }
        
        
    // Produce Monthly Graph
        
    for($i 1$i<13$i++){
            
    $sql "SELECT COUNT(access_date) FROM " $TABLEPREFIX "_data WHERE MONTH(access_date) = $i";
            
    $result mysql_query($sql);
            list(
    $monthhits[$i]) = mysql_fetch_row($result); 
        }
        
    $maxval max($monthhits);
        for(
    $i 1$i<13$i++){
            
    $monthhits[$i] = $monthhits[$i]*$barsize/$maxval 1;
        }
        
    ?>
        Total Hits: <?=$totalhits?><br>
        Hits Today: <?=$todayhits?><br><br>
        <table cellspacing=0 cellpadding=0 width=300 border=0>
            <tr>
                <td colspan=24 align=center><u>Hourly Graph</u></td>
            </tr>
            <tr>
                <?
                
    for($i 0$i<24$i++){
                    echo 
    "\t\t\t<td width=10 valign=bottom><img src=\"images/$i.gif\" width=10 height=" number_format($hourhits[$i], 0'.''') . " alt=0 border=0></td>\n";
                }
                
    ?>
            </tr>
        </table>
        <table cellspacing=0 cellpadding=0 width=300 border=0>
            <tr>
                <td colspan=12 align=center><u>Monthly Graph</u></td>
            </tr>
            <tr>
                <?
                
    for($i 1$i<13$i++){
                    echo 
    "\t\t\t<td width=10 valign=bottom><img src=\"images/$i.gif\" width=10 height=" number_format($monthhits[$i], 0'.''') . " alt=0 border=0></td>\n";
                }
                
    ?>
            </tr>
        </table>
        <table cellspacing=0 cellpadding=0 width=300 border=0>
            <tr>
                <td colspan=12 align=center><u>Daily Graph</u></td>
            </tr>
            <tr>
                <?
                
    for($i 1$i<8$i++){
                    echo 
    "\t\t\t<td width=10 valign=bottom><img src=\"images/$i.gif\" width=10 height=" number_format($dayhits[$i], 0'.''') . " alt=0 border=0></td>\n";
                }
                
    ?>
            </tr>
        </table>
        Browser Chart
        OS Chart
        <?
        
    break;
        default:
        
    // Regular Include - Make a Counter
        // Get Information About the User
        
    function iptype1 () {
         if (
    getenv("HTTP_CLIENT_IP")) {
           return 
    getenv("HTTP_CLIENT_IP");
         }
         else {
           return 
    "none";
         }
        }
        function 
    iptype2 () {
         if (
    getenv("HTTP_X_FORWARDED_FOR")) {
           return 
    getenv("HTTP_X_FORWARDED_FOR");
         }
         else {
           return 
    "none";
         }
        }
        function 
    iptype3 () {
         if (
    getenv("REMOTE_ADDR")) {
           return 
    getenv("REMOTE_ADDR");
         }
         else {
           return 
    "none";
         }
        }
        function 
    ip() {
         
    $ip1 iptype1();
          
    $ip2 iptype2();
         
    $ip3 iptype3();
         if (isset(
    $ip1) && $ip1 != "none" && $ip1 != "unknown") {
           return 
    $ip1;
         }
         elseif (isset(
    $ip2) && $ip2 != "none" && $ip2 != "unknown") {
           return 
    $ip2;
         }
         elseif (isset(
    $ip3) && $ip3 != "none" && $ip3 != "unknown") {
           return 
    $ip3;
         } 
          else {
          return 
    "none";
         }
        }

        
    $ip ip();
        
    $ip $_SERVER['REMOTE_ADDR'];
        
    $host gethostbyaddr($ip);
        
    $useragent $_SERVER['HTTP_USER_AGENT'];
        
    // Here's where I try to interpret their browser... it's an iffy method as is.
        
    $browser "Unknown";
        if (
    preg_match("/MSIE/i"$useragent) && !preg_match("/Gecko/i"$useragent) && !preg_match("/Opera/i"$useragent)) $browser="Internet Explorer";
        if (
    preg_match("/Mozilla/i"$useragent) && preg_match("/Gecko/"$useragent)) $browser="Mozilla";
        if (
    preg_match("/Opera/"$useragent)) $browser="Opera";
        
    // Again with the OS, iffy at best.  
        
    $os "Unknown";
        if (
    preg_match("/Windows/i"$useragent)) $os "Microsoft Windows";
        if (
    preg_match("/Macintosh/i"$useragent)) $os "Macintosh";
        if (
    preg_match("/X11/i"$useragent)) $os "*nix";
        
    $res "";
        
    $referer $_SERVER['HTTP_REFERER'];
        
    $page $_SERVER['PHP_SELF'];
        
    $datetime date('Y-m-d H:i:s');;

        
    // Decide whether to count hit or not
        
    foreach($IGNOREIP as $key => $value){
            if (
    $value == $ip$terminate TRUE;
        }
        if (!
    $terminate){
            
    $sql "SELECT user_ip, UNIX_TIMESTAMP(access_date) as access_date FROM " $TABLEPREFIX "_data WHERE user_ip = '$ip' ". ($TRACKPAGES "AND page = '$page'" "") . "ORDER BY access_date DESC LIMIT 0, 1";
            
    $result mysql_query($sql);
            
    $rs mysql_fetch_array($result);
            if((
    mktime() - $rs['access_date']) > $MAXTIMEmysql_query("INSERT INTO " $TABLEPREFIX "_data (access_date, user_ip, user_host, user_agent, user_browser, user_os, user_referer, page) VALUES (NOW(), '$ip', '$host', '$useragent', '$browser', '$os', '$referer', '$page')");
        }
        
    // All that's left it to spit out desired output
        
    $sql "SELECT user_ip FROM " $TABLEPREFIX "_data WHERE 1";
        
    $result mysql_query($sql);
        
    $totalhits mysql_num_rows($result);
        
    $sql "SELECT page FROM " $TABLEPREFIX "_data WHERE page = '$page'";
        
    $result mysql_query($sql);
        
    $pagehits mysql_num_rows($result);
        
    makecounter($totalhits$pagehits);
        
        break;
    }

    // Clean Up
    mysql_close();
    ?>
    The code to make the table in mysql:
    Code:
    CREATE TABLE wcounter_data (
    	access_date datetime NOT NULL default 'NOW()',
    	user_ip varchar(255) NOT NULL default 'unknown',
    	user_host varchar(255) NOT NULL default 'unknown',
    	user_agent varchar(255) NOT NULL default 'unknown',
    	user_referer varchar(255) NOT NULL default 'unknown',
    	page varchar(255) NOT NULL default 'unknown',
    	) TYPE=MyISAM;
    Thanks
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  2.  

  3. #2
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    P.S.

    Forgot the damned attachment... sorry for the stretch too
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:53 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com