We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

PHP Mobile Redirect

Development and Programming

gcylinder's avatar
gcylinder
49 posts
13 years ago
gcylinder's avatar gcylinder

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, '/');
      }
} ?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.