Switching off Mobile Site.

Ask about a PHP problem here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Switching off Mobile Site.

Post by FrederickGeek8 »

I am trying to setup a link that turns off mobile site on mobile devices (I am using this) using cookies but it does not seem to be working... Here is my code

Mobile website
[syntax=php]<?php
if(isset($_GET['normal_site'])){
setcookie('mobile_site', 'off', time() + 604800);
header('Location: http://nerd.net');
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Mobile Site | NERD.net</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta name='description' content=''>
<meta name='author' content='Fred Morlock'>

<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src='http://html5shim.googlecode.com/svn/trunk/html5.js'></script>
<![endif]-->
<script type='text/javascript'>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-33874985-1']);
_gaq.push(['_setDomainName', 'nerd.net']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
</head>
<body>
<h1>This is going to be the mobile site. </h1>
<footer><a href='index.php?normal_site=1'>Use Normal Site</a></footer>
</body>
</html>[/syntax]

init.inc.php (Lots of code. Only posting what is important)
[syntax=php]$detect = new Mobile_Detect();
if(isset($_COOKIE['mobile_site'])){
if((($_COOKIE['mobile_site']) === 'off')){
// do nothing
}
}else{
if ($detect->isMobile()) {
header('Location: http://m.nerd.net');
}
}[/syntax]
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Switching off Mobile Site.

Post by Helx »

Well, your cookies are fine.

Image
Though, I would probably say that you should put the cookie for the entire domain (nerd.net is the top-level domain, and should be assigned to it)

And I'm not at all sure... But give this a try:
[syntax=php] $detect = new Mobile_Detect();
if(isset($_COOKIE['mobile_site'])){
if($_COOKIE['mobile_site'] == 'off'){
// do nothing
}else{
if ($detect->isMobile()) {
header('Location: http://m.nerd.net');
}
}
[/syntax]
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Switching off Mobile Site.

Post by FrederickGeek8 »

That didn't help and I have no idea how to assign cookies to the top-level domain

EDIT: Nevermind I got it using [syntax=php]setcookie('mobile_site', 'off', time() + 604800, '/', 'nerd.net');[/syntax]
Post Reply