Jump to content

Recommended Posts

I've been working on a multimedia web site for a while and one feature that I'd like to add is the ability to "ban" specific domains. I do have this working... kinda

 

<?php

$myDNS_Bans = explode(',', "siteA.com,siteB.com,siteC.com"); // This will be pulled from the DB later - Testing for now
$ReqFrom = $_SERVER[HTTP_REFERER]; // Let's say the referer is siteB.com

foreach( $myDNS_Bans as $Banned ) {
	preg_match('@(.*?)('. $Banned .'?)(.*?)@is', $ReqFrom, $Match);

	if( $Match[2] ) {
		echo "This site is on our ban list!<br />\n";
	} else {
		echo "This site is NOT on our ban list!<br />\n";
	}

}
?>

 

All right so let's just say a file was requested from siteB.com well in the out put, you'd get:

 

This site is NOT on our ban list!

This site is on our ban list!

This site is NOT on our ban list!

 

I like to my code to be fast and right to the point. My problem is that if the site is on the ban list I need to stop the loop and move on to a function that redirects the request. How can I stop the loop? Or is there a better way this can be done.

 

Little background about this idea:

The idea is to give control to the person who uploaded the file. So let's say there is a site that they really don't want their image hot linked to. They'd simply enter 'what_ever_site_it_is.com' and it'd be saved to the database. Then when the file is called it checks the ban list against the referer. ( I DO NOT want to modify my htaccess file ).

 

Thanks guys.

Link to comment
https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/
Share on other sites

Thanks much drewjoh. I modified the if statement to check if there is a match, removed the echo, added a boolean and the break. Works great!.

 

I do have a question about the speed though. I highly doubt that some one will ban 100 domains but just in case, how much of a slow down would be expected? Well.. given the banned domain is at the end of the array. Would I be safe using this method?

100 shouldn't be a problem. If you were going upwards of maybe 5,000 or so, then I'd say it might be good to look for a better method (database).

 

Do look at the note at the bottom of preg_match: http://us2.php.net/manual/en/function.preg-match.php

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

 

If you can use strstr(), it is faster than preg_match.  :)

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.