Mobile Detect PHP

Hi,

I am trying to get my website to detect the type of browser that a user uses when they come to my site and depending on whether its a mobile/tablet they would be redirected to my mobile friendly site.

Below is the code I have to go into my home page for the normal site, I dont know exactly where to place it (i.e. head, body)

[php]<?php error_reporting(E_ALL && ~E_NOTICE); session_start(); header(“Vary: User-Agent, Accept”);

require_once ‘Mobile_Detect.php’; $detect = new Mobile_Detect();

if(in_array($GET[‘layout’], array(‘mobile’, ‘desktop’))){

setcookie(‘layout’, null); setcookie(‘layout’, $GET[‘layout’], time()+60602430, ‘/’, ‘www.homesandinvestments.com’); $layout = $GET[‘layout’];
} else {
if(!$COOKIE[‘layout’]){
$layout = ($detect->isMobile() ? ($detect->isTablet() ? ‘tablet’ : ‘mobile’) : ‘desktop’); setcookie(‘layout’, $layout, time()+60
602430, ‘/’, ‘.homesandinvestments.com’);
} else {
$layout = $COOKIE[‘layout’];
}
}
if ($detect->isMobile()) {

header(‘http://www.m.homesandinvestments.com’, true, 301);[/php]

Any advise is appreciated!

Thanks!!

There are headers being sent so this needs to be before ANY output to the browser (e.g. before your tag)

Hi M@tt - Thanks for the response! So if I understand you correctly this needs to come before the section?

For example:
[php]

<?php error_reporting(E_ALL && ~E_NOTICE); session_start(); header("Vary: User-Agent, Accept"); require_once 'Mobile_Detect.php'; $detect = new Mobile_Detect(); if(in_array($GET['layout'], array('mobile', 'desktop'))){ setcookie('layout', null); setcookie('layout', $GET['layout'], time()+60*60*24*30, '/', 'www.homesandinvestments.com'); $layout = $GET['layout']; } else { if(!$COOKIE['layout']){ $layout = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'mobile') : 'desktop'); setcookie('layout', $layout, time()+60*60*24*30, '/', '.homesandinvestments.com'); } else { $layout = $COOKIE['layout']; } } if ($detect->isMobile()) { header('http://www.m.homesandinvestments.com', true, 301); Homes and Investments | Home Page [/php] When i do this all the formatting get messed up.

It needs to be before ANY output to the browser. This above your opening <?php tag would cause the header functions to fail

I am sorry to be a pest but i don’t know what it means when you say “It needs to be before ANY output to the browser.”

Would you mind explaining that part?

Thanks!

<?php The first 3 lines before the PHP tag are considered output to the browser because they will be sent to the browser before the PHP code is executed. I'm not sure why you have two html tags either. [php] <?php error_reporting(E_ALL && ~E_NOTICE); session_start(); header("Vary: User-Agent, Accept"); require_once 'Mobile_Detect.php'; $detect = new Mobile_Detect(); if(in_array($GET['layout'], array('mobile', 'desktop'))){ setcookie('layout', null); setcookie('layout', $GET['layout'], time()+60*60*24*30, '/', 'www.homesandinvestments.com'); $layout = $GET['layout']; } else { if(!$COOKIE['layout']){ $layout = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'mobile') : 'desktop'); setcookie('layout', $layout, time()+60*60*24*30, '/', '.homesandinvestments.com'); } else { $layout = $COOKIE['layout']; } } if ($detect->isMobile()) { header('http://www.m.homesandinvestments.com', true, 301); ?> Homes and Investments | Home Page [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service