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..


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

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    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 Well-Known Member

    Messages:
    174
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    125
    #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:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    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:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    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,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #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 IP
  7. RahulSaini

    RahulSaini Well-Known Member

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #7
    Please don't store value in files. You can use database because files are slower and database is faster. Files are not safe to store IP Address of users/visitors
     
    RahulSaini, Oct 20, 2019 IP
  8. sbenjamin81

    sbenjamin81 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    I agree it's best practice to use a database. I wouldn't consider IP address as sensitive information though. A person's IP can be seen in any server log... For this purpose flat file would work just fine as long as it doesn't become too large. A database seems like a bit Overkill
     
    sbenjamin81, Oct 24, 2019 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    @RahulSaini
    Why is storing IP in a file unsafe? IP adress is nothing sensitive. Its not a password.

    There are 2 problems with that code.

    1. Your count.txt file is empty in the beginning, so this line is not doing anything:
    $current_inc = $current+1;

    Put a number in the count.txt file to start with, and this will be solved.


    2. You made a typo in this line:
    $filename="count.txt";
    $handle = fopen($filname,"r");

    Change to:
    $handle = fopen($filname,"r");

    You wrote "$filname". It should be "$filename"

    Do these, and it should work. Enjoy!

    When coding on wamp, keep your error reporting ON, It will catch a lot of errors like that $filename typo etc.
     
    Last edited by a moderator: Oct 25, 2019
    JEET, Oct 25, 2019 IP
    sarahk likes this.