Jump to content

Problem with a function


Matpatnik

Recommended Posts

Hi guys, I created a little function that check the ip address and write it into a file after the third attempt.I'm pretty sure that the problem is in the for(), it duplicate the ip only if this one is not the same as the first one in the file.Of course it will be easier to use the db but that's not my goal here.

// TODO check this function right now (It duplicate the ip)// check ip address, after the third tantative it insert the ip, if ip is there it eather delete the line or update the time() depending on the $timefunction ip_check($time=3600) {	global $access;	$access = true;	$delete = false;	$time = (time() + $time);	$contents = read_file('logfailed','urip','txt');	if ($contents != 0) {		$arr = explode("\r\n",$contents);		$count = count($arr);				for ($i=0;$i<$count;$i++) {			list($ip,$date) = explode(',',$arr[$i]);						if ($date < time()) {				$arr = array_slice($arr, 1);				$delete = true;			} else if ($_SERVER['REMOTE_ADDR'] == $ip) {				$arr[$i] = $_SERVER['REMOTE_ADDR'] .','. $time;				$access = false;			} else if ($_SESSION['try2log'] > 1) {				$arr[$count] = $_SERVER['REMOTE_ADDR'] .','. $time;				$access = false;			}		}		if ($access == false || $delete == true) {			write_file(implode("\r\n",$arr),'logfailed','urip','txt','w');		}	} else if ($_SESSION['try2log'] > 1) {		$arr = $_SERVER['REMOTE_ADDR'] .','. $time;		$access = false;		write_file($arr,'logfailed','urip','txt','w');	}}

Thank you to enlighten me on thatMatpatnik

Link to comment
Share on other sites

It work fine for the first and second line but a soon as I try again with the first ip I ending with 4 line which the 2 last one are the same as the first one.It does update the first line but it also duplicate it twice at the end of the file.

function trim_value(&$value) { 	$value = trim($value); }// TODO check this function right now (It duplicate the ip)// check ip address, after the third tantative it insert the ip, if ip is there it eather delete the line or update the time() depending on the $timefunction ip_check($time=3600) {	global $access;	$access = true;	$delete = false;	$time = (time() + $time);	$contents = read_file('logfailed','urip','txt');	if ($contents != 0) {		$arr = explode("\n",$contents);		$count = count($arr);				for ($i=0;$i<$count;$i++) {			list($ip,$date) = explode(',',$arr[$i]);						if ($date < time()) {				$arr = array_slice($arr, 1);				$delete = true;			} else if ($_SERVER['REMOTE_ADDR'] == $ip) {				$arr[$i] = $_SERVER['REMOTE_ADDR'] .','. $time;				$access = false;			} else if ($_SESSION['try2log'] > 1) {				$arr[$count] = $_SERVER['REMOTE_ADDR'] .','. $time;				$access = false;			}		}		if ($access == false || $delete == true) {			if (is_array($arr)) {				array_walk($arr, 'trim_value');			} else {				trim_value($arr);			}			write_file(implode("\n",$arr),'logfailed','urip','txt','w');		}	} else if ($_SESSION['try2log'] > 1) {		$arr = trim($_SERVER['REMOTE_ADDR'] .','. $time);		$access = false;		write_file($arr,'logfailed','urip','txt','w');	}}

Link to comment
Share on other sites

I'm not sure why you're using array_slice. You're using it with a constant offset, so every time you do that you are shifting the first element off the array, is that what you're trying to do? Because that means that $arr is getting smaller as you loop through it, so by the time you get to the end of $count you're going to be looking at undefined indexes. You're also going to skip elements. If you are looking at element 0, so $i is 0, and you do array_slice to remove element 0, the next time through the loop $i is set to 1 so you will be looking at element 1, but you will skip the new element 0 because it used to be 1 before you moved the first element off the array. So that's probably why it's duplicating things, because every time it shifts an element off the array it skips the next element, it never looks at it.

Link to comment
Share on other sites

I fixed the problem!it was coming from this part:

else if ($_SESSION['try2log'] > 1) { $arr[$count] = $_SERVER['REMOTE_ADDR'] .','. $time; $access = false; }
It was duplicated the line because each value of the array was not always equal to the ip but the session was true so I separate themand instead of using array_slice I just empty the value and at the end I used array_filter.What do you guys think?
// find the first occurrence of a part of a string in an arrayfunction strin_array($array,$string) {	$i = 0;	foreach ($array as $value) {		if (strstr($value,$string)) {			$i++;		}	}	return ($i == 0)? false : true;}// check ip address, after the third tantative it insert the ip, if ip is there it eather delete the line or update the time() depending on the $timefunction ip_check($time=3600) {	global $access;	$access = true;	$delete = false;	$time = (time() + $time);	$contents = read_file('logfailed','urip','txt');	if ($contents != 0) {		$arr = explode("\n",$contents);		$count = count($arr);				if (strin_array($arr, $_SERVER['REMOTE_ADDR'])) {			for ($i=0;$i<$count;$i++) {				list($ip,$date) = explode(',',$arr[$i]);								if ($date < time()) {					$arr[$i] = '';					$delete = true;				} else if ($_SERVER['REMOTE_ADDR'] == $ip) {					$arr[$i] = $_SERVER['REMOTE_ADDR'] .','. $time;					$access = false;				}			}		} else if ($_SESSION['try2log'] > 1) {			$arr[$count] = $_SERVER['REMOTE_ADDR'] .','. $time;			$access = false;		}				if ($access == false || $delete == true) {			if (is_array($arr)) {				array_filter($arr);				array_walk($arr, 'trim_value');			} else {				trim_value($arr);			}			write_file(implode("\n",$arr),'logfailed','urip','txt','w');		}	} else if ($_SESSION['try2log'] > 1) {		$arr = trim($_SERVER['REMOTE_ADDR'] .','. $time);		$access = false;		write_file($arr,'logfailed','urip','txt','w');	}}

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...