Salis Posted October 15, 2007 Share Posted October 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/ Share on other sites More sharing options...
drewjoh Posted October 15, 2007 Share Posted October 15, 2007 This will stop your loop: <?php break; More information here: http://us.php.net/break Just put that in the if( $Match[2] ) statement. Quote Link to comment https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/#findComment-370081 Share on other sites More sharing options...
Salis Posted October 15, 2007 Author Share Posted October 15, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/#findComment-370085 Share on other sites More sharing options...
drewjoh Posted October 15, 2007 Share Posted October 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/#findComment-370088 Share on other sites More sharing options...
Salis Posted October 15, 2007 Author Share Posted October 15, 2007 I agree. The ban list will be pulled from a database once i finalize the script. But even then I cannot think why any one would take the time to ban 5,000 domains... let alone 20 even. Quote Link to comment https://forums.phpfreaks.com/topic/73350-solved-domain-ban-list-foreach/#findComment-370092 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.