-1

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:

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.

4

2 回答 2

1

我一直在尝试做同样的事情,我终于让它工作了。我用了和你一样的方法,所以我不确定你的为什么不起作用......

在我的移动网站上,我有一个按钮可以查看完整的网站。此按钮链接到 www.fullsite.com/setmobile.php

setmobile.php 是一个简单的 php 文件,如下所示:

<?php
    setcookie("mobile", "full", time()+3600);
    header("Location:http://www.fullsite.com");
?>

它设置 cookie 并转到主站点。主站点有以下php代码:

<?php
if (!isset($_COOKIE["mobile"])){
    (...detect mobile script)
}
?>

它对我有用,但看起来与您使用的方法相同,所以我不确定为什么您的方法不起作用...

您现在可能已经找到了解决方案,但如果没有,我希望这可以帮助您:)

祝你好运,安德鲁

于 2013-09-16T09:18:45.240 回答
0

Cookie 具有“域”限制,

例如,默认情况下, 的cookiehttp://mobile.example.com不可http://www.example.com

但是,它用于创建 cookie 的域的所有子域。例如http://a.mobile.example.comhttp://b.mobile.example.com

您可以在设置 cookie 时指定应该为哪个域创建 cookie。在此处阅读 PHP 手册:

http://php.net/manual/en/function.setcookie.php

不确定这是否是您问题的完整答案,但由于您提到多个域名,这可能是问题的一部分

希望这可以帮助

于 2013-03-24T18:47:52.507 回答