Hack Forums
[PHP] Block all useragent except one - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Block all useragent except one (/showthread.php?tid=5334848)



Block all useragent except one - 3yZtwglFSCUs - Jul 11, 2016

Hello can someone help me with this i want to block all useragents except one which is "Globelexic 2.7"

PHP Code

<?php 
if(!empty($_SERVER['HTTP_USER_AGENT'])) {
    
$userAgents = array("Google", "Slurp", "MSNBot", "ia_archiver", "Yandex", "Rambler");
    if(
preg_match('/' . implode('|', $userAgents) . '/i', $_SERVER['HTTP_USER_AGENT'])) {
        
header('HTTP/1.0 404 Not Found');
        exit;
    }
} 



* - Chickendipper - Jul 11, 2016

PHP Code

<?php 
if ($_SERVER['HTTP_USER_AGENT'] !== "Globelexic 2.7") header('HTTP/1.0 404 Not Found'); 



* - 3yZtwglFSCUs - Jul 11, 2016

(Jul 11, 2016 04:25 AM)Chickendipper Wrote:  
PHP Code

<?php 
if ($_SERVER['HTTP_USER_AGENT'] !== "Globelexic 2.7") header('HTTP/1.0 404 Not Found'); 

page still showing :(


* - Kylos - Jul 11, 2016

(Jul 11, 2016 04:32 AM)3yZtwglFSCUs Wrote:  page still showing :(

Try using exit after header?


* - 3yZtwglFSCUs - Jul 11, 2016

it's perfect :D

if ($_SERVER['HTTP_USER_AGENT'] !== "Globelexic 2.7") {
header('HTTP/1.0 404 Not Found');
exit;
}