1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development PHP Hit Counter Problems

Discussion in 'Software' started by Dinh, 11 Jun 2006.

  1. Dinh

    Dinh What's a Dremel?

    Joined:
    27 Jun 2004
    Posts:
    810
    Likes Received:
    0
    I am having a problem with a friend's hit counter script, which was modified a few times from the original. He basicly wants the "Today's Unique Hits" and "Total Uniques" only to be visible, which is accomplished. But the counter itself is stuck at 1, no matter what. I've tried changing permissions on stats.txt, but it simply does not work because it does not log the ip? Any help would greatly be appreciated.

    How the log file supposed to look like(x's are there for a reason):
    Code:
    7x.247.231.220|30June2005
    7x.247.231.220|30June2005
    7x.247.231.220|30June2005
    
    How it looks like now:
    Code:
    |11June2006
    |11June2006
    |11June2006
    |11June2006
    
    Code:
    <?php
    // Our log file;
    $counter = "stats.txt";
    
    // Date logging;
    $today = getdate();
    $month = $today[month];
    $mday = $today[mday];
    $year = $today[year];
    $current_date = $mday . $month . $year;
    
    
    // Log visit;
    $fp = fopen($counter, "a");
    $line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";
    $size = strlen($line);
    fputs($fp, $line, $size);
    fclose($fp);
    
    // Read log file into array;
    $contents = file($counter);
    
    // Total hits;
    $total_hits = sizeof($contents);
    
    // Total hosts;
    $total_hosts = array();
    for ($i=0;$i<sizeof($contents);$i++) {
    	$entry = explode("|", $contents[$i]);
    	array_push($total_hosts, $entry[0]);
    }
    $total_hosts_size = sizeof(array_unique($total_hosts));
    
    // Daily hits;
    $daily_hits = array();
    for ($i=0;$i<sizeof($contents);$i++) {
    	$entry = explode("|", $contents[$i]);
    	if ($current_date == chop($entry[1])) {
    		array_push($daily_hits, $entry[0]);
    	}
    }
    $daily_hits_size = sizeof($daily_hits);
    
    // Daily hosts;
    $daily_hosts = array();
    for ($i=0;$i<sizeof($contents);$i++) {
    	$entry = explode("|", $contents[$i]);
    	if ($current_date == chop($entry[1])) {
    		array_push($daily_hosts, $entry[0]);
    	}
    }
    $daily_hosts_size = sizeof(array_unique($daily_hosts));
    
    ?>
    <? echo "
    <strong>Unique hits:</strong>  " . $total_hosts_size . "<br>&nbsp;
    <strong>Todays unique hits:</strong> " . $daily_hosts_size; 
    ?>
    
     
  2. dfhaii

    dfhaii internets

    Joined:
    24 Mar 2002
    Posts:
    520
    Likes Received:
    0
    It's not logging the IP because $REMOTE_ADDR is empty, you want to replace it with $_SERVER['REMOTE_ADDR']

    Col
     
  3. FuzzyOne

    FuzzyOne

    Joined:
    19 Sep 2002
    Posts:
    1,839
    Likes Received:
    37
    Which will return there proxy ip if there using a transparent one.

    Use this instead

    PHP:
    function getIP() {
        if (isset(
    $_SERVER)) {
            if (isset(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {
                return 
    $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                return 
    $_SERVER['REMOTE_ADDR'];
            }
        } else {
            if (isset(
    $GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDER_FOR'])) {
                return 
    $GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDED_FOR'];
            } else {
                return 
    $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];
            }
        }
    }
     

Share This Page