Adding a cookie to allow a visitor from mobile device to continue along on the main website instead of the mobile multisite

In my previous post (http://www.mattyoung.com.au/automatic-mobile-device-detection-drupal) I outlined a method using php in the settings.php file to get a drupal 5 multisite to detect a mobile device and redirect the user to the mobile subdomain site but then if they want to visit the main site, they need to be able to view it. The method outlined in that post allows the user to view the requested page on the main site but when they try and access another page they will then be redirected back to the mobile subomain. In order to allow them to continue to navigate in the main domain you need to set a cookie which indicates that they want to view the main domain not just the mobile site.

Using jquery to add a cookie on the mobile site

In order to set the cookie using jquery you need the jquery cookie plugin which you can get from http://plugins.jquery.com/project/Cookie. Once you have the plugin you can download http://www.mattyoung.com.au/files/mobilepreference.js.txt and remove the .txt extension so that it is a javascript file.

Next, you need to call those javascript files. I am using a zen theme and did this in the subtheme template.php file. I added the following lines to template.php to call the scripts on every page of the mobile site:

drupal_add_js('sites/all/jquery.cookie.js', 'core', 'header', FALSE, TRUE);
drupal_add_js('sites/all/mobilepreference.js', 'core', 'header', FALSE, TRUE);

This adds the javascript files to the head of the page and allows them to be cached.

Once this is done ll you need to do is to add a class of "fullsitevisit" to any link to your main site.

Now you are adding a cookie when that link is clicked. Next you need the main site to check for that cookie prior to redirecting to the mobile site.

modifying the previous php snippet added to the settings.php page

change the php code at the start of the settings.php file of your main site to:

include('sites/all/checkmobile.php');
$from = getenv("HTTP_REFERER");
if ($from == "http://m.yourdomain.com/")
/* If refering domain is your mobile domain we don't want to send them back there so do nothing*/
	{if (isset($_COOKIE["mobilepreference"])){}
	else (setcookie ("mobilepreference", "nomobile", time()+43200, "/", ".yourdomain.com"));{}}
else
	{if (isset($_COOKIE["mobilepreference"])){}
	elseif(checkmobile()) header("Location:http://m.yourdomain.com");}

This first of all checks if the referring domain is the mobile subdomain and if it is checks if the cookie is set. If it is it does nothing and the visitor continues on the the main site. If the cookie is not set (if the user has javascript disabled or doesn't have javascript abilities on their mobile) the cookie is set and they are directed on to the main page. If the mobile user has come from elsewhere and visits and they have the cookie set from a previous visit they will be shown the main site. If they haven't got the cookie the checkmobile script is run and if they're mobile they go to the mobile subdomain.

The reason that you need to initial jquery bit is that if you just use the php part the first click on the link on the mobile subdomain sets the cookie, but then sends the client back to the mobile site. So, it appears to the user that the link didn't work. If they again click that link a second time they will go to the main site as the cookie is now set. Using jquery you get there on the first click as expected.

comment

Its great news for me any way nice to hear your content. Right now i am trying to expand my business of gold with my studies. Best of luck....

comment

Hello, i am cliff with you and today i will disclose some new authentic medicines and i hope these are really effective. cialis are completely without any side effects.

comment

Hello, i am cliff with you and today i will disclose some new authentic medicines and i hope these are really effective. cialis are completely without any side effects.