Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php redirect doesn't work properly

I got a simple redirect on my homepage, which reacts to the user being on a smartphone or tablett. The Class is working and gives back a true when the side is opened on a smartphone.

Therefore if i write an echo in the if-statement it gets echoed. But the redirection doesn't work and i can't make a sense of it. Anyone any clue what i missed here?

include ('includes/Mobiledetecter.php');                 
$detect = new Mobiledetecter;                                  
                                 //
if($detect->isMobile() or $detect->isTablet()) {           
    header('Location: http://www.example.com/');
} 
like image 965
Banjo Avatar asked Mar 13 '26 19:03

Banjo


1 Answers

Exit immediately after setting the header if there is nothing else to do:

if($detect->isMobile() or $detect->isTablet()) {           
    header('Location: http://www.example.com/');
    exit;
} 

Also consider that headers only work if you haven't already sent output to the browser, either explicitly (eg: echo) or implicitly (eg: by having anything including blank space before the <?php tag that contains this header)

like image 50
BeetleJuice Avatar answered Mar 15 '26 10:03

BeetleJuice