Need help with a simple code !

  1. #1
    vxdesigns is offline Junior Member

    Cool Need help with a simple code !

    Hey people how's life treating ya, i'm hoping everyone's having a good weekend.
    I have a very simple PHP stats script which counts " Page hits, Unique hits, Todays Page hits, Todays unique hits" Here's the coding for it

    Code:
    <body>
    <font face="Verdana" size="1">
    <?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 "
    Page hits:<b> " . $total_hits . "</b><br><br>
    Unique hits: <b> " . $total_hosts_size . "</b><br><br>
    Todays Page hits: <b> " . $daily_hits_size . "</b><br><br>
    Todays unique hits: <b>" . $daily_hosts_size; 
    ?>
    It counts the page hits properly but it does not count the unique page hits properly meaning it doesn't count the unique page hits after the first unique hit although i;ve asked people in different areas with different ips to visit the page so i can be sure that something is wrong with the code.

    Page hits: 10

    Unique hits: 1

    Todays Page hits: 10

    Todays unique hits: 1

    You see it keeps counting page hits but doesn't count uniques after the first one. I'm assuming somethings wrong with the coding, so please can someone take a look at the following coding and provide a fix ? I will greatly greatly appreciate it !


  2. #2
    HappyBeaver is offline Bea*ering Away!
    I'm no expert but there is a difference in the code compared to the other entries...

    Shouldn't it be

    Code:
    Todays unique hits: <b>" . $daily_hosts_size . "
    instead of:

    Todays unique hits: <b>" . $daily_hosts_size;

  3. #3
    vxdesigns is offline Junior Member
    Quote Originally Posted by HappyBeaver View Post
    I'm no expert but there is a difference in the code compared to the other entries...

    Shouldn't it be

    Code:
    Todays unique hits: <b>" . $daily_hosts_size . "
    instead of:
    No that gives an error :/

  4. #4
    D-A-L is offline D-A-L Administrator
    Tackle the Web with $5.99 .COM's from Go Daddy!
    Quote Originally Posted by vxdesigns View Post
    No that gives an error :/
    You would still need a ";" at the end.

+ Reply to Thread