hello i was just wondering if it is possible to remove text from a text file. I have a text file with ip addresses and i want to remove a persons ip address when they go to my webpage. if it is possible could someone please show me how to do so. Thanks

-Madmee

    post the contents of your textfile
    and the php code that is used for storing those ip
    that would make it possible to help you quicker

      ok here is what the text file contains. the text file is called a.txt

      ignore this
      216.86.101.9
      207.216.252.214

      and here is my php the php file is called joe3.php3

      <?php
      
      $ip=@$REMOTE_ADDR;
      $writeip="\n";
      $filename="a.txt";
      
      $fileopen=fopen($filename,"r");
      $contents=fread($fileopen,'10000000');
      fclose($fileopen);
      
      $pos=strpos($contents,$ip);
      
      $fp=fopen($filename,"a+");
      
      if ($pos==false) {
      echo "False";
      fwrite($fp,$writeip);
      fwrite($fp,$ip);
      } else {
      fwrite($fp,"");
      }
      
      ?>
      

      when someone goes onto a different page in my website i want to remove their ip address if it is in the text file.
      thanks again

      -Madmee

        Is there only 1 IP per line, and nothing but the IP on that line? If so, code like this would work:

        <?PHP
        $fpath = 'test.txt';
        $file = file($fpath) or die('Could not open file!');
        
        for($i = 0; $i < count($file); $i++) {
        	if(trim($file[$i]) == getenv("REMOTE_ADDR")) unset($file[$i]);
        }
        
        $fp = fopen($fpath, 'w') or die('Could not open file!');
        fwrite($fp, implode('', $file));
        fclose($fp);
        ?>

          Yes there is only one ip per line and there is nothing else on that line, but when i put the code in it did not remove the ip address from my text file, i know i have to change the "Test.txt" to my .txt file but do i have to change anything else to make it work properly? i'll keep trying different things to try and get it working.

          EDIT:
          haha i accidentally changed "test.txt" to the whole url, "http://mars.ark.com/~happygng/a.txt" it works now thank you very much.

          -Madmee

            to make your user's IP text file a little more secure
            create a subfolder 'user_data'
            and protect that folder from access by
            putting this file
            .htaccess
            along with 'userip.txt' in that subfolder folder

            Otherwise your visitors can read that 'usersip.txt'
            by usin http://yourwebsite/userip.txt

            Contents of '.htaccess'

            Deny from All

            Only your php scripts can access your userdata in that subfolder.
            This will work if server uses Apache.
            In any folder, Apache first looks for '.htaccess' to see who can access files from http
            .htaccess = short for http-access

            .htaccess Tutorial

              Originally posted by bradgrafelman
              Assuming his host allows .htaccess files! :p

              That is first thing mentioned in Tutorial link.
              :p

                halojoy - thinks better give complete info, especially in Newbies 🙂

                  Thanks for the .htaccess info, i will definatly use this on my site, but i need people to be able to access the .txt file.
                  thanks

                  -Madmee

                    Use authentication (.htpasswd file, etc.) to require username/password, OR add an "Allow from" followed by the user(s) IP(s).

                    EDIT: Or... place the .txt file outside the root of your web directory, using a PHP script to access and output it ONLY have having authenticated the user against a database (flat file, hardcoded, or software such as MySQL).

                    To me, this is like killing a fly with a canon. The .htpasswd (HTTP Authentication) seems to be the ideal solution to me.

                      Write a Reply...