Jump to content

PHP method has opposite outcome to what I expected?

Master Disaster

Yeah, so I'm not the worlds best coder and I'm fairly new to PHP but this one has me very confused

$show_results = FALSE;
$results_html = "";
$final_result_html = "<hr>";
$array_search = NULL;
$warnarray = include 'blacklist.php';
$error_text = "";

if(isset( $_GET['q'])) { // if there's a search query, show the results for it
    $query = urlencode($_GET["q"]);
	$array_search = (in_array(strval($query), $warnarray));
	if(!$array_search != NULL) {
	$error_text .= "$query is in the blacklist";
	echo "<script type='text/javascript'>alert('$error_text');</script>";
	}
    $show_results = TRUE;
    $search_url = "https://html.duckduckgo.com/html?q=" . $query;
    if(!$results_html = file_get_contents($search_url)) {
        $error_text .=  "Failed to get results, sorry :( <br>";
    }

I included my array.php file and defined my array_search variable to null, performed an in_array search of the string value of $query on my array then checked if array search is not equal to null and if so fire an error.

 

The problem is it works in reverse, if the search query is in the array it shows the result with no error, if the query is not in the array it fires the error?

 

I'm 99.9% being a dumb ass here.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

1. don't do stuff like 

$warnarray = include 'blacklist.php';

put whatever is there in a function then use require_once to include the php file in your script and then fill the variable $warnarray by accessing the function inside the blacklist file.

 

2. You may want to use $_REQUEST which copies both $_POST and $_GET arrays, so your code would work in either ways.

3. in_array is case sensitive for strings, so it's kinda dumb to use it if you're trying to blacklist specific words (user could just use uppercase, lowercase to go around it)

4. in_array returns true or false, not null ... maybe use if $array_search !== FALSE  (use !== because it checks that it's actually boolean false and not just something that can be converted to false)

5. file_get_contents assumes you have filters enabled to be able to download pages, you should consider enabling the curl extension and building the request yourself

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, mariushm said:

1. don't do stuff like 


$warnarray = include 'blacklist.php';

put whatever is there in a function then use require_once to include the php file in your script and then fill the variable $warnarray by accessing the function inside the blacklist file.

 

2. You may want to use $_REQUEST which copies both $_POST and $_GET arrays, so your code would work in either ways.

3. in_array is case sensitive for strings, so it's kinda dumb to use it if you're trying to blacklist specific words (user could just use uppercase, lowercase to go around it)

4. in_array returns true or false, not null ... maybe use if $array_search !== FALSE  (use !== because it checks that it's actually boolean false and not just something that can be converted to false)

5. file_get_contents assumes you have filters enabled to be able to download pages, you should consider enabling the curl extension and building the request yourself

 

Thanks, I'm still new to PHP and I'm trying to learn as I go.

 

I basically forked a pretty simple open source project from GIT and I'm trying to get a few ideas I had working, more as a learning exercise than anything else.

 

The intent isn't really to blacklist anything, just to fire a warning that the search query is on a list of sites that don't work.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×