I have a client for whom I built an ExpressionEngine site, and they hired another company to do a mobile version of the site (separate, and static). The development outfit that did the mobile site is asking me to put a PHP mobile redirect script into index.php. They said if it’s put as the first PHP code in the template, especially at the very top of the document (even above the <html> tag), it should work.
I had to enable PHP in the home page, and process it on input for it to work. I’m concerned about the security implications of this, given the warning you get when you enable PHP. Any advice would be appreciated.
Here’s the code:
<?php
// Specify mobile site URL here
$redirectUrl = 'http://m.pacificbag.com';
if(isset($_GET['fullsite']))
{
// A web browser version is requested - set a cookie to save on API requests
setcookie('isMobile', "0", time()+3600, '/');
}
if(isset($_COOKIE['isMobile']) && $_COOKIE['isMobile'] == true && !isset($_GET['fullsite']))
{
header('Location:'.$redirectUrl);
exit();
}
else if(isset($_COOKIE['isMobile']) && $_COOKIE['isMobile'] == false && !isset($_GET['fullsite']))
{
// Any web traffic processing here
}
else
{
// Setup device headers
$headers = '';
foreach($_SERVER as $key => $val)
{
$headers .= $key.':'.$val.'|';
}
// Create a post fields array to pass to Wapple
$postfields = array(
'devKey' => '85396d96f149636dd66ebc96ae87d5e3',
'headers' => $headers
);
// Do CURL communication
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://webservices.wapple.net/isMobileDevice.php');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
$result = curl_exec($c);
curl_close($c);
// Perform action based on the result
if($result && !isset($_GET['fullsite']))
{
// Set a cookie so we don't use a gazillion API requests
setcookie('isMobile', "1", time()+3600, '/');
// Forwarding around to mobile site
header('Location:'.$redirectUrl);
exit();
}
else
{
// A web browser - set a cookie to save on API requests
setcookie('isMobile', "0", time()+3600, '/');
}
} ?>Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.