Jump to content

[SOLVED] More efficient rewrite


miless

Recommended Posts

Is there any way to rewrite this code to make it more efficient ?

 

$host = gethostbyaddr($ip); // (usually returns something like blah.blah.blah.host.com)

if(
stristr($host,".host1.com") || 
stristr($host,".host2.com") || 
stristr($host,".host3.com") || 
stristr($host,".host4.com") || 
stristr($host,".host5.com") || 
stristr($host,".host6.com") || 
stristr($host,".host7.com") || 
stristr($host,".host8.com") || 
stristr($host,".host9.com") || 
stristr($host,".host10.com")){
exit;
}

Link to comment
https://forums.phpfreaks.com/topic/109550-solved-more-efficient-rewrite/
Share on other sites

You can put the host names inside an array, and then loop through them. The code will be a bit shorter, but I don't think it will be faster (but strpos() is faster than stristr() nonetheless):

 

<?php
$host = strtolower(gethostbyaddr($ip)); // (usually returns something like blah.blah.blah.host.com)

$hosts = array('.host1.com', '.host2.com', '.host3.com', '.host4.com', '.host5.com', '.host6.com', '.host7.com', '.host8.com', '.host9.com', '.host10.com');

foreach ($hosts as $h) {
if (strpos($host, $h) !== false) {
	exit;
}
}
?>

 

Edit: Added strtolower().

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.