clown[NOR] Posted May 1, 2007 Share Posted May 1, 2007 I just cant get this thing to work.. It keeps on adding my IP even tho it's allready in the file.. <?php function checkIP($ip) { echo "Current IP: $ip<br><br>"; $filename = "ips.txt"; $lines = file($filename); $i=1; while ($lines[$i] != "") { if ($lines[$i] == $ip) { return true; exit; } echo $lines[$i]."<br>"; $i++; } return false; } function countIP() { $filename = "ips.txt"; $ip = $_SERVER['REMOTE_ADDR']; $chkIP = checkIP($ip); if ($chkIP == false) { $write = $ip."\n"; $fh = fopen($filename, 'a+'); fwrite($fh, $write); fclose($fh); } else { return "IP Exists!"; } } ?> Link to comment https://forums.phpfreaks.com/topic/49385-solved-please-someone-help-me-out-here/ Share on other sites More sharing options...
kenrbnsn Posted May 1, 2007 Share Posted May 1, 2007 In the function checkIP, you need to use the trim() function to trim all whitespace and newline characters from the input: <?php function checkIP($ip) { echo "Current IP: $ip<br><br>"; $lines = file('ips.txt'); // why introduce another variable that's only used once? foreach ($lines as $line) { // foreach works better here if (trim($line) == $ip) return true; // don't need the "exit" echo $line."<br>"; } return false; } ?> In the function countIP, the "if" statement should be: <?php if (!$chkIP) { $write = $ip."\n"; $fh = fopen($filename, 'a+'); fwrite($fh, $write); fclose($fh); return "IP Does not exist"; } else return "IP Exists!"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/49385-solved-please-someone-help-me-out-here/#findComment-242033 Share on other sites More sharing options...
clown[NOR] Posted May 1, 2007 Author Share Posted May 1, 2007 Thanks for the answer Ken.. Worked perfect! Link to comment https://forums.phpfreaks.com/topic/49385-solved-please-someone-help-me-out-here/#findComment-242184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.