I have a website, with a main version and a mobile version. I'm trying to create a "view full site" button that does two things when clicked:
- Set a cookie so the site can remember that the button was clicked.
- Navigate to the main (not mobile) version of the site.
Here is how I went about doing it:
- Create the button.
- On click, navigate to a processor page with PHP that sets the cookie, and then redirects to the main version of the site.
- Add PHP to the main version of the site that checks if the "view full site" button has been clicked.
Here is what my site is built with:
- jQuery Mobile (site is built with this)
- PHP mobile detect
- jQuery cookie
But it's not working.
On the index of the main version of the site, on the header above the opening HTML tag, I have this:
if(!isset($_COOKIE['dontDoMob'])){
//its not set so check for device type.
include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() ) {
//"its some kind of mobile device so redirect to mobile";
header("Location:http://myforecyte.com/m/");
exit();
}
}
On the index of the mobile site, I added the button, and set on click to set the cookie via jQuery, and then redirect to home page which should continue to load the main version of the site thanks to the cookie.
But it didn't work: it wouldn't even redirect. I wasn't sure if it was jQuery Mobile's fault, so I separated everything. What I did was set the "view full site" link within jQuery mobile to go to a separate processor page (like <a href="processorpage.php" rel="external">
), which has this code on it:
setcookie('dontDoMob', 'yes');
header("Location:http://www.myforecyte.com");
But it always goes back to the mobile version. I see the main site's URL for a split second, and then it redirects to the mobile again. As if the cookie wasn't set.
I think it has something to do with setting the cookie on a global scale or something like that.
Note: I checked to make sure that the process of setting the cookie worked fine, with this code:
if(isset($_COOKIE['dontDoMob'])){
echo "its set yo";
} else {
echo "its not set";
}
It works and tells me "its set yo". When I delete all cookies and retest with firebug, it always works accordingly.