Categorias

Contador de visitas utilizando arquivo com PHP

Com este código é possível criar um contador de visitas para seu site utilizando arquivos.

Fácil de instalar e implementar em seu site, este código funciona como uma imagem em seu site. Consulte os comentários do código para ver como é sua implementação.

 
//    
// Counter with red background 
//    
// or: 
//    
//    
// Counter with red background, green foreground and 4 digits 
//    
// or: 
//    
//    
// Counter with transparent background and red foreground 
//    
// 

    Header("Content-type: image/gif"); 
     
    require("colors.php"); 
     
    // Load the gd-library 
    // under Windooze use this one 
    // dl("php3_gd.dll"); 
    // under UNIX use this one 
    dl("php3_gd.so"); 

    // some default-values 
    $Font     = 5; 
    $BGColor  = GetColor("black"); 
    $BGTrans  = False; 
    $FGColor  = GetColor("white"); 
    $FGTrans  = False; 
    $Length   = 7; 

    // get environment 
    $query_string = getenv("QUERY_STRING"); 
     
    // parse environment 
    // split query-string 
    $env_array = split("&", $query_string); 
     
    // split in key=value and convert %XX 
    while (list($key, $val) = each($env_array)) 
    { 
        // split 
        list($name, $wert) = split("=", $val); 
         
        // replace %XX by character 
        $name = urldecode($name); 
        $wert = urldecode($wert); 
         
        // write to $cgivars 
        $CGIVars[$name] = $wert; 
    } 
     
    // eventually replace the default-values by the given parameters 
    if ($CGIVars["BGColor"]) 
    { 
        if (ereg("([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars["BGColor"], $tmp)) 
        { 
            $BGColor["red"]   = $tmp[1]; 
            $BGColor["green"] = $tmp[2]; 
            $BGColor["blue"]  = $tmp[3]; 
        } 
        else if (eregi("transparent", $CGIVars["BGColor"])) 
        { 
            $BGTrans = True; 
        } 
        else 
        { 
            $BGColor = GetColor($CGIVars["BGColor"]); 
        } 
    } 

    if ($CGIVars["FGColor"]) 
    { 
        if (ereg("([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars["FGColor"], $tmp)) 
        { 
            $FGColor["red"]   = $tmp[1]; 
            $FGColor["green"] = $tmp[2]; 
            $FGColor["blue"]  = $tmp[3]; 
        } 
        else if (eregi("transparent", $CGIVars["FGColor"])) 
        { 
            $FGTrans = True; 
        } 
        else 
        { 
            $FGColor = GetColor($CGIVars["FGColor"]); 
        } 
    } 

    if ($CGIVars["Length"]) 
    { 
        $Length = $CGIVars["Length"]; 
    } 

    // calculate size of image 
    $SizeX = $Length * 13; 
    $SizeY = 16; 
     
    // read counter-file 
    if (file_exists("counter.txt")) 
    { 
        $fp = fopen("counter.txt", "rt"); 
        while ($Line = fgets($fp, 999)) 
        { 
            // split lines into identifier/counter 
            if (ereg("([^ ]*) *([0-9]*)", $Line, $tmp)) 
            { 
                $CArr["$tmp[1]"] = $tmp[2]; 
            } 
        } 
         
        // close file 
        fclose($fp); 
         
        // get counter 
        $Counter = $CArr[$CGIVars["Identifier"]]; 
        $Counter += 1; 
        $CArr[$CGIVars["Identifier"]] = $Counter; 
    } 
    else 
    { 
        // the new counter is initialized with 1 
        $CArr[$CGIVars["Identifier"]] = 1; 
        $Counter = 1; 
    } 
     
    // write counter file 
    $fp = fopen("counter.txt", "wt"); 
     
    // output array elements 
    reset($CArr); 
    while (list($Key, $Value) = each($CArr)) 
    { 
        $tmp = sprintf("%s %dn", $Key, $Value); 
        fwrite($fp, $tmp); 
    } 
     
    // close file 
    fclose($fp); 
     
    // Counter mit führenden Nullen auffüllen 
    // fill counter with leading 0´s 
    $Counter = sprintf("%0".$Length."d", $Counter); 
     
    // create image 
    $img = ImageCreate($SizeX + 4, $SizeY + 4); 
     
    // use interlace 
    ImageInterlace($img, 1); 
     
    // transparent color for separators 
    $trans = ImageColorAllocate($img, 1, 1, 1); 
    ImageColorTransparent($img, $trans); 

    // fill background 
    if ($BGTrans) 
    { 
        ImageFill($img, 1, 1, $trans); 
    } 
    else 
    { 
        $col = ImageColorAllocate($img, $BGColor["red"], $BGColor["green"], 
                                  $BGColor["blue"]); 
        ImageFill($img, 1, 1, $col); 
    } 
     
    // output digits 
    if ($FGTrans) 
    { 
        $col = $trans; 
    } 
    else 
    { 
        $col = ImageColorAllocate($img, $FGColor["red"], $FGColor["green"], 
                                  $FGColor["blue"]); 
    } 

    $PosX = 0; 
    for ($i = 1; $i <= strlen($Counter); $i++) 
    { 
        ImageString($img, $Font, $PosX + 3, 2 + $i % 2,  
            substr($Counter, $i - 1, 1), $col); 
     
    if ($i != 1) 
    { 
        // draw separator 
        ImageLine($img, $PosX, 0, $PosX, $SizeY + 4, $trans); 
    } 
     
    $PosX += 13; 
    } 
     
    // output image 
    ImageGif($img); 
    ImageDestroy($img); 
?>