0
<?php 
  echo '<b>Your ip has now been logged:</b> ';
  echo $_SERVER["REMOTE_ADDR"]; 
$myip = "144.141.53.98"; //so it does not log your IP and spam up the log file
 //echo ' Your ip: '; 

if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
echo '' . $_SERVER["REMOTE_ADDR"] . ' '; 
//echo "You are using Localhost";
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
  echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' '; 
//echo "Your ip is forwarded";
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
  echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' '; 
//echo "You ip is not forwarded";
} 

$file = fopen("ips.txt", "a+");
fwrite($file,$_SERVER["REMOTE_ADDR" ]."\n");

?>

Okay now this line of code below

$myip = "144.141.53.98"; //so it does not log your IP and spam up the log file

and I want the if a ip has already been recorded in the log file then it will not record the ip second time

Please help?

user3102403
  • 1
  • 1
  • 1
  • Look here: http://stackoverflow.com/questions/9059026/php-check-if-file-contains-a-string – Aydin Hassan Dec 14 '13 at 14:47
  • 1
    I highly recommend another form of data representation such as JSON, XML, or SQL. In all these cases, it's fairly easy to check for duplicates and is way faster than plain text. For JSON, you just need to use an array, and serialize them with the appropriate functions. – Dave Chen Dec 14 '13 at 14:49

1 Answers1

0
<?php

 echo '<b>Your ip has now been logged:</b> ';
  echo $_SERVER["REMOTE_ADDR"]; 
$myip = "144.141.53.98"; //so it does not log your IP and spam up the log file
 //echo ' Your ip: '; 
$ip = $_SERVER['REMOTE_ADDR'];

if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
echo '' . $_SERVER["REMOTE_ADDR"] . ' '; 
//echo "You are using Localhost";
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    { 
  echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' '; 
//echo "Your ip is forwarded";
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
  echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' '; 
//echo "You ip is not forwarded";
} 

$file = file_get_contents('ips.txt');

// No Duplicate IP Address's

if(strpos($file, $ip) !== false)
{

    die("<br><br>Your IP Address: ".$ip." has already been logged.");

}

//Write IP To file

$file = fopen("ips.txt", "a+");`enter code here`
fwrite($file,$_SERVER["REMOTE_ADDR" ]."\n");
fclose($file);

?>
Syst3m
  • 1
  • 1
    Welcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you [include some explanation along with it](http://meta.stackexchange.com/q/114762/159034) to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Aug 26 '15 at 18:32