Hi All,

I just posted here about writing to a text file and resolved it, but now I have a new issue.

I am trying to search the text file and match a users IP, from my get IP code and I tried...

$test = ereg($ip, $logread, $regs);

And that will find a match, but then I must figure out what position in the array ($regs[1]) and that will not work as far as I can see.

Then I tried...

$test = preg_match($ip, $logread, $regs);

and I will have the same results.

What I am trying to do is see if the IP is already in the text file, then assign that to $reg and compare it to $ip.

Does someone see a better way or something I missed?

thanks again,
Don

    What does your regex look like?

    Post more code with capitalized PHP UBB tags.

      Kudose wrote:

      Post more code with capitalized PHP UBB tags.

      Actually, the issue is that he's missing the '/' on the last tag.

      The bbcode tags work just like HTML... except instead of <php>...</php> it's [php]...[/php].

        Sorry about the missing /.

        Here is the entire block of code...

        error_reporting (E_ALL); 
        function getRemoteAddr()
        {
            if ( isset($_SERVER['HTTP_CLIENT_IP']) ) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif ( isset($_SERVER['REMOTE_ADDR']) ) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
            return $ip;
        } 
        $ip = getRemoteAddr();
        echo 'Your IP is '.getRemoteAddr();
        
        $ipfile = "iplog.txt";
        
        $newaddress = "\n".$ip;
        
        $log = fopen($ipfile, "r");
        $logread = fread($log, 1024);
        
        $test = preg_match($ip, $logread, $regs);
        fclose($log);
        
        if($regs==$ip){
        echo '<br />$regs[0]='.$regs[0].'<br />';
        }
        
        $log = fopen($ipfile, "a");
        fwrite($log, $newaddress);
        fclose($log);
        

        Does that shed some more light on the subject.

        Thanks for helping,
        Don

          If you're using preg_match then remember that you need to start and end the pattern with some sort of delimiter character, typically /.

          However, you probably don't need preg_match() anyway, since $ip is going to be a literal IP address. In that case [man]strpos[/man] will be a much faster method of seeing if it's present. You won't capture the matched IP as you would with preg_match, but that doesn't matter, because you already know what the IP is.

          $test = strpos($logread, $ip)!==false;
          if($test)
          {
          // $ip found in $logread
          }
          

            Woah ... Weedpacket changed his Avatar ... and it's scary lookin.

              Write a Reply...