1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What's wrong in this code ?

Discussion in 'PHP' started by sash_007, Sep 23, 2019.

  1. #1
    hello friends,

    I am trying to make a simple unique hit counter with ip address and .txt file

    but somehow this code is not working ..me using wamp server to test the code
    this is my count.php
    <?php
    
    function hit_count(){
        $ip_address =$_SERVER["REMOTE_ADDR"];
    
    $ip_file = file("ip.txt");
    foreach($ip_file as $ip){
         $ip_single = trim($ip);
         if($ip_address == $ip_single){
           
             $found = true;
             break;
         }else{
             $found = false;
         }
       
    }
        if($found==false){
            $filename="count.txt";
            $handle = fopen($filname,"r");
            $current =fread($handle,filesize($filename));
            //echo $current;
            fclose($handle);
          
            $current_inc = $current+1;
          
            $handle = fopen($filname,"w");
            fwrite($handle,$current_inc);
            fclose($handle);
          
            $handle = fopen("ip.txt","a");
            fwrite($handle,$ip_address . "\n");
            fclose($handle);
          
          
        }
      
    }
    ?>
    PHP:
    and this is my index.php

    <?php
    //hit counter
    include "count.php";
    
    hit_count();
    
    ?>
    
    PHP:
    i have ip.txt(this is blank) file and count.txt(value set to 0) file on the server
    but when i refresh page ..counter is not counting page hits..
    SEMrush

    any ideas?
     
    Last edited: Sep 23, 2019
    sash_007, Sep 23, 2019 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    25,260
    Likes Received:
    3,439
    Best Answers:
    100
    Trophy Points:
    665
    #2
    We'll start with the niggly stuff
    • use filter_input(INPUT_SERVER, 'REMOTE_ADDR') to get the ip address
    • on line 19 you have a typo - referring to $filename as $filname
      $filename="count.txt";
      $handle = fopen($filname,"r");
    • You file handling seems off, too much opening and unnecessary closing
    Then there's the content of count.txt. I'm not sure what you think you're recording - literally just the number of hits?
     
    sarahk, Sep 23, 2019 IP
  3. sash_007

    sash_007 Active Member

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    thanks but it still doesn't work

    here is the modified code
    <?php
    
    function hit_count(){
        $ip_address =filter_input(INPUT_SERVER, "REMOTE_ADDR");
    
    $ip_file = file("ip.txt");
    foreach($ip_file as $ip){
         $ip_single = trim($ip);
         if($ip_address == $ip_single){
           
             $found = true;
             break;
         }else{
             $found = false;
         }
       
    }
        if($found==false){
            $filename="count.txt";
            $handle = fopen($filename,"r");
            $current =fread($handle,filesize($filename));
            //echo $current;
            fclose($handle);
          
            $current_inc = $current+1;
          
            $handle = fopen($filname,"w");
            fwrite($handle,$current_inc);
            fclose($handle);
          
            $handle = fopen("ip.txt","a");
            fwrite($handle,$ip_address . "\n");
            fclose($handle);
          
          
        }
      
    }
    ?>
    PHP:
    me still learning php..so this is is just tutorial stuff
     
    Last edited: Sep 23, 2019
    sash_007, Sep 23, 2019 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    25,260
    Likes Received:
    3,439
    Best Answers:
    100
    Trophy Points:
    665
    #4
    A big part of programming is knowing how to debug. Chuck some var_export() commands in there to see what variables have. Let us know which lines aren't doing what you expect and describe your expectation versus what your code is doing.
     
    sarahk, Sep 23, 2019 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #5
    1) why not just have the true condition RETURN? Would skip a bunch of excess junk.

    Why are you opening and closing files willy-nilly and reading the entire file?

    Why read the entire file if you might be able to short-circuit out prematurely?

    
    <?php
    
    function hitCount(){
    
    	$ipAddress =$_SERVER["REMOTE_ADDR"];
    	
    	$handle = fopen('ip.txt', 'r+');
    	while ($line = fgets($handle)) {
    		if ($line == $ipAddress) {
    			fclose($handle);
    			return;
    		}
    	}
    	fputs($ipAddress . "\r\n");
    	fclose($handle);
    	
    	$handle = fopen('count.txt', 'r+');
    	$current = fread($handle);
    	fseek($handle, 0);
    	fwrite($handle, $current++);
    	fclose($handle);
    	
    }
    
    Code (markup):
    Untested, might have typo's, but is likely what you're trying to do in a more compact and efficient form.
     
    deathshadow, Sep 24, 2019 IP
    sarahk likes this.
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,248
    Likes Received:
    23
    Best Answers:
    7
    Trophy Points:
    140
    #6
    1) Issue with line 21 - $current =fread($handle,filesize($filename)); You are trying to read an empty file therefore it fails. I would combine lines 21,22 and 23 into $current = file_get_contents($filename);

    2) Typo in line 27 - $handle = fopen($filname,"w"); should be $handle = fopen($filename,"w");

    Once I made the above changes it worked for me.
     
    stephan2307, Oct 19, 2019 at 3:36 PM IP