0

Hey everyone I'm using a code I found in another question that says it was answered correctly and working. It's not working for me though and I have tried it out on multiple devices.

The full code can be found here(this is the code library): https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.php

The question is here: Best way to redirect single php page for mobile devices with PHP/Javascript

And my code is:

<?php require('includes/mobileRedirect.php'); ?>
<?php
    $detect = new Mobile_Detect;
    if ( $detect->isMobile() ) {
 header('Location: http://m.jollyrogerpcs.com/');
 exit;
}
?>

The problem seems to be around the "header" area. If I replace the header with a echo('mobile'); it works, but no matter what I do I cannot seem to get mobile to redirect.

EDIT: Problem solved by moving the code before the DOCTYPE.

Community
  • 1
  • 1
Jesse Elser
  • 974
  • 2
  • 11
  • 39
  • Have you checked the error logs? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Apr 20 '15 at 18:00
  • I got these: Warning: Cannot modify header information - headers already sent by (output started at /home/jollyrogerpcs/public_html/index.php:3) in /home/jollyrogerpcs/public_html/includes/head.php on line 4 Warning: Cannot modify header information - headers already sent by (output started at /home/jollyrogerpcs/public_html/index.php:3) in /home/jollyrogerpcs/public_html/includes/head.php on line 5 Warning: Cannot modify header information - headers already sent by (output started at /home/jollyrogerpcs/public_html/index.php:3) in /home/jollyrogerpcs/public_html/includes/head.php on line 6 – Jesse Elser Apr 20 '15 at 18:04
  • That is these lines: header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); – Jesse Elser Apr 20 '15 at 18:06
  • The file that you're trying to redirect from has already sent output to the browser. Once that happens no redirect can take place. – Jay Blanchard Apr 20 '15 at 18:07
  • possible duplicate of [header location not working in my php code](http://stackoverflow.com/questions/12525251/header-location-not-working-in-my-php-code) – Jay Blanchard Apr 20 '15 at 18:08
  • I moved it to before everything else(right after the doctype) and nothing still happens. – Jesse Elser Apr 20 '15 at 18:10
  • Put it in a file by itself and try it. – Jay Blanchard Apr 20 '15 at 18:10
  • 1
    Nevermind I moved it to BEFORE the DOCTYPE and it works. Thanks Jay. You mentioning it already outputting made me think to move it there. – Jesse Elser Apr 20 '15 at 18:11

1 Answers1

0

Moved this code to before the DOCTYPE and it works like a charm.

<?php require('includes/mobileRedirect.php'); 
$detect = new Mobile_Detect;
 if ( $detect->isMobile() ) {
  header('Location: http://m.jollyrogerpcs.com/');
}
?>
Jesse Elser
  • 974
  • 2
  • 11
  • 39