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);
			}
Link to comment
Share on other sites

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) {
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.