Jump to content

[SOLVED] Check array for a string... Much confusion - Help appreciated.


lol_php

Recommended Posts

Hi all.

 

I am trying to set up a blacklist for a service I'm making. My current objective is to call up a .txt file which contains one domain name per line. I then take the user's input from a HTTP post and check to see if it contains the domain name. Here is my current non working script:

 

//take user's input (example: http://www.somesite.com/files/photo.jpg)
$filename = $HTTP_POST_VARS['filename'];

//load blacklist in case of abuse
$blacklist= file('blacklist.txt');

//check submitted image against blacklisted domains
foreach($blacklist as $site){

if (strstr($filename,$site) !== FALSE) {
echo "<div><h3>This site is blacklisted</h3></div>";
exit();
}
}

 

This doesn't seem to work at all. I think it has something to do with the variable types, but I am too unfamiliar with this for Google to be a viable solution. Can you please help me?

 

Thank you.

CODE IS NOT TESTED - check the regular expression if it doesn't work!

 

ok, you're trying to take

 

http://www.somesite.com/files/photo.jpg

 

and see if the domain

 

somesite.com

 

is in blacklist.txt

 

ok, here is what I assume is in blacklist.txt - if not, you will have to change the regex

somesite.com
someothersite.com

 

and the code to check - you are close, but you should really use preg_match

 

so:

<?php
// $filename = $HTTP_POST_VARS['filename']; //commented this out in my code so I can see if it works
$filename = 'http://www.somesite.com/files/photo.jpg';

$blacklist = file('blacklist.txt');

foreach($filename as $needle) { 
  preg_match('/^http:\/\/www\.(.*\..*)\/.*$/',$needle,$regs);
    foreach($blacklist as $haystack) {
     preg_match('/$regs[1]/',$haystack,$results);
     if ($results) {
echo "<div><h3>This site is blacklisted</h3></div>";
        exit();
     } else {
        echo 'Your site $site has been processed!'
     }
   }
}
?>

Thanks for the quick reply yzerman  :)

 

I have tried your code, but initially it resulted in this error: "Warning: Invalid argument supplied for foreach()" I Googled that and realized it is because $filename is not an array, but a plain and simple string. Not knowing where to go from there, I just chopped out the initial foreach($filename as $needle)  section to see what happened. Not surprisingly it didn't work, rather it printed "Your site $site has been processed!" several times and accepted the blacklisted image.

 

I will find out about and check the "regular expression" as you suggested, as well as make sense of this code you've kindly provided.

Finally got it sorted!

 

Here was the final solution:

 

//load blacklist in case of abuse
$blacklist= file('directory/blacklist.txt');

//check submitted image against blacklisted domains
foreach($blacklist as $bannedurl){
$bannedurl = rtrim($bannedurl);
if (strstr($image,$bannedurl) !== FALSE) {
echo "<div><h3>Sorry, this domain is blacklisted</h3></div>";
echo $home;
exit();
}
}
//end blacklist check

 

The key point in that was rtrim(). If you want to read out line-breaked data into an array from a text file, make sure you rtrim them. I got a little clued up when I found out the very last blacklisted URL would work, whereas any previous ones in the list wouldn't.

 

Thanks for the help again!

 

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.