Jump to content

[SOLVED] Please.. Someone help me out here


clown[NOR]

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.