miless Posted June 10, 2008 Share Posted June 10, 2008 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 More sharing options...
trq Posted June 10, 2008 Share Posted June 10, 2008 Probably not. Link to comment https://forums.phpfreaks.com/topic/109550-solved-more-efficient-rewrite/#findComment-561920 Share on other sites More sharing options...
thebadbad Posted June 10, 2008 Share Posted June 10, 2008 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(). Link to comment https://forums.phpfreaks.com/topic/109550-solved-more-efficient-rewrite/#findComment-561937 Share on other sites More sharing options...
miless Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you thebadbad. You can also scrap strtolower and use stripos if you use PHP5. Maybe even scrap !==false) Link to comment https://forums.phpfreaks.com/topic/109550-solved-more-efficient-rewrite/#findComment-562038 Share on other sites More sharing options...
DarkWater Posted June 10, 2008 Share Posted June 10, 2008 You need the !== false (Note the two == AND the !). This is because if strpos finds the string at the first position, it returns zero, which is usually assumed false. Link to comment https://forums.phpfreaks.com/topic/109550-solved-more-efficient-rewrite/#findComment-562055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.