Stylw Posted February 5, 2015 Share Posted February 5, 2015 (edited) 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 February 5, 2015 by Stylw Quote Link to comment https://forums.phpfreaks.com/topic/294401-difficulty-with-comparing-user-input-to-lines-in-txt-files/ Share on other sites More sharing options...
Barand Posted February 5, 2015 Share Posted February 5, 2015 (edited) 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 February 5, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/294401-difficulty-with-comparing-user-input-to-lines-in-txt-files/#findComment-1504956 Share on other sites More sharing options...
AbraCadaver Posted February 6, 2015 Share Posted February 6, 2015 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/294401-difficulty-with-comparing-user-input-to-lines-in-txt-files/#findComment-1505110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.