Jump to content

how to search string for phrase using array


jason310771

Recommended Posts

I can not seem to find out why this code will not work.

I am wanting it to first set a dummy string to 'notin' then
check if any of the strings in the array are in the $fromlink4 variable that has the contents of gethostbyaddr($_SERVER['REMOTE_ADDR']), if just one is set the dummy string $isinarray to 'isin'
the next step is to see if $isinarray is set to 'notin' if not then store the visitor details.

at the moment it stores everything even if any of the array strings are in $fromlink4

$fromlink4 = $_SERVER['REMOTE_ADDR'] != "" ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : "" . $_SERVER['REMOTE_ADDR'] . "";
$needle = array('bot', 'crawl', 'spider', 'superkabel', 'telenet', 'your-server' ,'whois', 'unassigned', 'fastwebserver', 'opendub', 'sagonet', 'search');
$isinarray = "notin";
//echo("isinarray = false<br>");
		foreach($needle as $item){
		//echo("<br>item = " . $item . "  ");
				if(strpos("$fromlink4", $item) == true) { // 'false' = IS in string
					$isinarray = "isin";
					//echo("isinarray = true  ");
				}
		}
			if($isinarray == "notin") {
			//echo("<br><br><br><br>isinarray = true       add to the DB<br>");
			$send_error = "INSERT INTO visitTracker (`data`, `allvars`, `time`, `theip`, `fromlink4`) VALUES ('" . $mysqli->real_escape_string($data) . "', '" . $mysqli->real_escape_string($allvars) . "', '" . $mysqli->real_escape_string($datetimenow) . "', '" . $mysqli->real_escape_string($ipAddress) . "', '" . $mysqli->real_escape_string($fromlink4) . "')";
			db_query($mysqli, $send_error);
			}

your code will fail if $fromlink4 starts with one of the items in your array, otherwise it should work. Also, $fromlink4 is already a string so there is no need for the the double quotes round it in the strpos() parameters. A better test is this which will work even if the found string is in position 0.

if(strpos($fromlink4, $item) !== false) {

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.