Jump to content

Dannyeo

Recommended Posts

Hi there

 

I am trying to redirect mobile users automatically from the desktop to the moble version of a website.

 

I have been provided with the following Curl code which I have placed in the Header.php file of my Wordpress site. I have confirmed with my Hosting provider who have said they do support Curl statements.

 

Unfortunately I am getting a parse error in line 8: Parse error: syntax error, unexpected ';', expecting ')'

 

<?php
$domain_www = 'http://mydomain.co.uk';
$domain_mobi = 'http://m.mydomain.co.uk';
// Manually specify page by page redirects
$mobile_urls = array($domain_www.'/' => $domain_mobi.'/', // Homepage
$domain_www.'/page.php' => $domain_mobi.'/page.php',
// Leave the code below unchanged to execute the mobile redirect
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.someotherdomainsomwhere.com/detect-mobile.php"); // returns if mobile
$fields_string = 'usr='.urlencode($_SERVER['HTTP_USER_AGENT']).'';
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$response = htmlentities($output);
if(trim($response) == '1')
{
foreach ($mobile_urls as $i => $value)
{
if(strtolower('http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"])==strtolower($i))
{
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".strtolower($value) );
exit();
}
}
}
?>

 

Any help would be much appreciated. Even if I remove the ; from line 8 I then get an error on line stating

 

Parse error: syntax error, unexpected T_STRING, expecting ')'

 

Also, where would you recommend storing this code on a Wordpress installation?

 

Thanks

DY

Edited by Dannyeo
Link to comment
Share on other sites

The error is because you didn't end the array statement before that line. This could be avoided in the future by 1. using a decent editor which highlights syntax errors, and 2. structuring your code to be more easily read.

 

Compare

$mobile_urls = array($domain_www.'/' => $domain_mobi.'/', // Homepage
$domain_www.'/page.php' => $domain_mobi.'/page.php',

 

To

$mobile_urls = array(
   $domain_www.'/'        => $domain_mobi.'/',
   $domain_www.'/page.php' => $domain_mobi.'/page.php'
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.