2

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/');
} 
Banjo
  • 73
  • 5
  • You're not, by any chance, outputting anything before the `header()` are you? http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – CD001 Jul 19 '16 at 14:28
  • Headers come before body. That's the rule. A redirect header means the client doesn't see anything intermediate. – apokryfos Jul 19 '16 at 14:30

4 Answers4

6

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)

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
2

Make sure your code is right at the very top of your Script before any output and also that your opening php tags has no white-space character(s) before it. In essence, your script should look something like this:

<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php & NO ECHO BEFORE header(...)

    include ('includes/Mobiledetecter.php');                 
    $detect = new Mobiledetecter;                                  
                             //
    if($detect->isMobile() or $detect->isTablet()) {           
        header("Location: http://www.example.com/");
        exit;
    } 
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • upvote: good thinking with the space before ` – BeetleJuice Jul 19 '16 at 14:39
  • @BeetleJuice It may sound somehow strange to hear this but honestly, you are a very sincere & straightforward person... just an observation from about 2 - 3 encounters with you so far... and thanks a lot for the up-vote plus commendation ;-) – Poiz Jul 19 '16 at 14:45
  • Much appreciated. *<>* – BeetleJuice Jul 19 '16 at 14:49
1

Further to what everyone else is correctly saying that you can't both echo and do a server-side redirect, here's a way to say something and then redirect:

include ('includes/Mobiledetecter.php');                 
$detect = new Mobiledetecter;                                  
                                 //
if($detect->isMobile() or $detect->isTablet()) {           
     echo "Redirecting you to the example website";
     echo "Click <a href='http://www.example.com/'>here</a> if you are not redirected within 5 seconds";        
     echo "<script> setTimeout(function () { window.location.href='http://www.example.com/'; }, 3000);</script>"
     exit;
} 
apokryfos
  • 38,771
  • 9
  • 70
  • 114
1

Try ob_end_clean function before header : http://php.net/manual/fr/function.ob-end-clean.php

Lionnel
  • 17
  • 4
  • If output buffering is on, the `header` will be set properly without the need for this function (just tested this). If output buffering is off, this function won't fix the output problem because there would be no buffer to clean. So I don't know what circumstance this function actually improves. Am I misunderstanding it? – BeetleJuice Jul 19 '16 at 14:45