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.

Link to comment
Share on other sites

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!'
     }
   }
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

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.