Jump to content

Difficulty with comparing user input to lines in .txt files


Stylw

Recommended Posts

 I'm rather new to PHP, and I've run into a problem develeoping this script that I can't figure out. It's fairly simple in that it fetches the user input via a search box and cross references it against a blacklist and whitelist in the form of two seperate .txt. files. However, the enclosed code seems to ignore the second if statement and will always return "unknown website" even if users' input is in the blacklist. The first if statement works correctly though, so I'm a little bit lost as to where I've gone wrong.

 

I'm also not sure how I shoud approach implementing case sensentivity and checking if the user input contains a specfiic word that's on a line. For example, if a user inputted phpfreaks into the search box it would still come out as legimtmate even though I didn't specifically state phpfreaks (it would be the full url, e.g. phpfreaks.com)  in the whitelist txt file. From my research I would think the strpos function or preg_match might work, but I'm not sure how I should put it in there.

<?php
$search = $_POST['search'];

$blacklist = file_get_contents("illegitimatesites.txt");
$whitelist = file_get_contents("legitimatesites.txt");

$blacklist = explode("\n", $blacklist);
$whitelist = explode("\n", $whitelist);

if(in_array($_POST['search'], $whitelist)){ //checks if site is in array
	echo "This website is an authorised distributor"; }

	else{

		if(in_array($_POST['search'], $blacklist)){ //checks if site is in array
				echo "This website is NOT an authorised distributor"; }
			
	else{

		echo "Unknown website";
}
}
 ?>
Edited by Stylw
Link to comment
Share on other sites

stripos() is case insensitive.

$blacklist = file('blacklist.txt', FILE_SKIP_EMPTY_LINES);
$whitelist = file('whitelist.txt', FILE_SKIP_EMPTY_LINES);

$srch = 'phpFreaks';

if (array_filter($whitelist, function($var) use ($srch) {return stripos($var,$srch)!==false;}))
    echo "Allowed";
elseif (array_filter($blacklist, function($var) use ($srch) {return stripos($var,$srch)!==false;}))
    echo "Not allowed";
else
    echo "Unknown site";
Edited by Barand
Link to comment
Share on other sites

Another one:

if(preg_grep("/$search/i", file("legitimatesites.txt"))) {
    echo "This website is an authorised distributor";
} elseif(preg_grep("/$search/i", file("illegitimatesites.txt"))) {
    echo "This website is NOT an authorised distributor";
} else {
    echo "Unknown website";
}

 

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.