Unique IP Entry in Text File & Help Combining Scripts

Discussion in 'PHP' started by American, Sep 17, 2006.

  1. #1
    <?php
    
    
    
    $filename = "ips.txt" ;
    
    if(!file_exists($filename)){
    $fd = fopen ($filename , "w+");
    fclose($fd);
    }
    
    $file = file($filename);
    $file = array_unique($file);
    $hits = count($file);
    
    
    $TextFile = "hits.txt";
    $Count = trim(file_get_contents($TextFile));
    $FP = fopen($TextFile, "r");
    $Count=fgets($FP, 4096);
    fclose ($FP);
    settype($Count, "integer");
    $Count++;
    if ($FP = fopen ($TextFile, "w")){
    fwrite ($FP, $Count);
    fclose ($FP);
    
    }
    
    
    
    
    $image = "counterpic.png";
    $im = imagecreatefrompng($image);
    $red = ImageColorAllocate ($im, 255, 0, 0);
    $blue = ImageColorAllocate ($im, 0, 0, 255);
    $hit = "$Count";
    $ip = $_SERVER["REMOTE_ADDR"];
    $user = $_REQUEST["username"];
    
    ImageString($im, 2, 150, 1, " Welcome, $user ", $blue);
    ImageString($im, 2, 130, 18, " Your IP: $ip", $blue);
    ImageString($im, 2, 150, 36, " Unique hits: $hits ", $blue); 
    ImageString($im, 2, 147, 50, " Total hits: $hit ", $blue); 
    header("Content-Type: image/png");
    Imagepng($im,'',100);
    ImageDestroy ($im);
    
    
    
    $fd = fopen ($filename , "r");
    $fstring = fread ($fd , filesize ($filename)) ;
    fclose($fd) ;
    $fd = fopen ($filename , "w");
    $fcounted = $fstring."".getenv("REMOTE_ADDR")."
    ";
    $fout= fwrite ($fd , $fcounted );
    fclose($fd);
    
    
    
    ?>
    PHP:

    This is something I copied from several sources... I have my vbulletin system to fetch the image /counter/index.php?username=$bbuserinfo[username] to display their username in the image.

    Now, the ips.txt file will add a single line for every IP address and will log every IP address even if it's already listed. I need a way for it to only add it if it's not listed, to reduce file size. It can quickly, within a day, get up to 200 MB+ in size, which would be a problem in the long run...


    I have an external file (though it produces errors, it still works) that will do the task, but I've yet failed to merge the two. It is the following:

    <?php
    
    
    $filename = "ips.txt";
    if(!file_exists($filename)){
    $fd = fopen ($filename , "w+");
    fclose($fd);
    }
    
    $file_data = file($filename);
    $file_data = array_unique($file_data);
    
    $new_file_data = implode("",$file_data);
    
    $handle = fopen($filename , "w+");
    $new_handle = fopen($filename , "w+");
    
    $fd = fopen ($filename , "r");
    $fstring = fread ($fd , filesize ($filename)) ;
    fclose($fd);
    $fd = fopen ($filename , "w");
    $fout= fwrite ($fd , $new_file_data );
    fclose($fd);
    ?>
    PHP:
    The error it gives is:

    Warning: fread(): Length parameter must be greater than 0. in /home/content/A/m/e/American2005/html/counter/admin.php on line 19

    I've tried several ways to fix it, however, all times have resulted in either (A.) the script not working properly (maybe not adding the prior data to the new output) or (B.) more errors.

    If I try to merge them by replacing functions, I always end it making it not add the prior data and simply add the last hit resulting in the output image to always say 1 unique hit.


    All suggestions are ever so greatly appreciated.

    ~American
     
    American, Sep 17, 2006 IP
  2. Mrblogs

    Mrblogs Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change fread to fgets.

    You could alternatively use a Database table, with the Key field being the IP address.
     
    Mrblogs, Sep 18, 2006 IP
  3. American

    American Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Now I get Warning: fgets(): Length parameter must be greater than 0. in /home/content/A/m/e/American2005/html/counter/admin.php on line 19


    I think a MySQL database is a great idea; do you have any idea on what the new code would be? And is there something that would submit the current IPs to the database to comply with the code?

    I'm not that great with PHP so forgive me there...
     
    American, Sep 18, 2006 IP