Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic Visitor Counter Sig

    In this tutorial I will teach you how to make this.


    To start off create a new folder with ".jpg" at the end (to trick browsers to think its an image), I will be using visitorsig.jpg

    Here is going to be the file structure
    (bold is a folder, non-bold is a file)
    visitorsig.jpg
    --index.php
    --background.jpg
    --sigcounter.txt
    --fonts
    ----arial.ttf
    Now inside that folder create an "Index.php" This will be where all the code is kept.

    This is what Index.php will look like
    Code:
    <?php
    Header ('Content-type: image/jpeg');
    
    //START OF COUNTING
    $counter = "sigcounter.txt";
    
    $today = getdate();
    $month = $today[month];
    $mday = $today[mday];
    $year = $today[year];
    $current_date = $mday . $month . $year;
    
    $fp = fopen($counter, "a");
    $line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";
    $size = strlen($line);
    fputs($fp, $line, $size);
    fclose($fp);
    
    $contents = file($counter);
    
    $total_hits = sizeof($contents);
    
    $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 = 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));
    
    //START OF IMAGE
    $img_width = 300;
    $img_height = 92;
    
    $image = imagecreatefromjpeg("background.jpg");
    
    $white = imagecolorallocate($image, 255, 255, 255);
    
    $font = './fonts/arial.ttf';
    
    ImageTTFText ($image, 8, 0, 10, 20, $white, $font, "Image Hits: ".$total_hits);
    ImageTTFText ($image, 8, 0, 10, 40, $white, $font, "Unique hits: ".$total_hosts_size);
    ImageTTFText ($image, 8, 0, 10, 60, $white, $font, "Todays Image hits: ".$daily_hits_size);
    ImageTTFText ($image, 8, 0, 10, 80, $white, $font, "Todays unique hits: ".$daily_hosts_size);
    
    imagepng($image);
    imagedestroy($image);
    
    ?>
    Be sure to not leave any spaces before <?php or after ?>, it will cause an error.

    Now create a background.jpg, it needs to be 300x92
    I will be using this background.

    http://img228.imageshack.us/img228/2...kgroundzb8.jpg

    The sigcounter.txt will be created when the image is first looked at.

    Now create a fonts folder under your .jpg folder and inside upload arial.ttf, this can be found in c:/windows/fonts/arial.tff

    Now just go to http://YOUR DOMAIN NAME HERE/visitorsig.jpg!
    Last edited by TexasMd91; 06-03-2007 at 02:13 AM.

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    330
    Thanks
    0
    Thanked 13 Times in 13 Posts
    You can do it in one loop instead of three
    PHP Code:
    ...

    $total_hosts = array();
    $daily_hits = array();
    $daily_hosts = array();

    for (
    $i=0;$i<sizeof($contents);$i++) {
        
    $entry explode("|"$contents[$i]);
        
    array_push($total_hosts$entry[0]);
        if (
    $current_date == chop($entry[1])) {
            
    array_push($daily_hits$entry[0]);
        }
        if (
    $current_date == chop($entry[1])) {
            
    array_push($daily_hosts$entry[0]);
        }
    }

    $total_hosts_size sizeof(array_unique($total_hosts));
    $daily_hits_size sizeof($daily_hits);
    $daily_hosts_size sizeof(array_unique($daily_hosts));

    //START OF IMAGE

    ... 
    Be sure to not leave any spaces before <?php or after ?>, it will cause an error.
    If you don't put a ?> whitespace at the end won't cause an error.


  •  

    Posting Permissions

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