【问题标题】:How to detect if user is using an app or browser to access my site?如何检测用户是否正在使用应用程序或浏览器访问我的网站?
【发布时间】:2014-01-14 22:33:15
【问题描述】:

大家好,我正在使用具有桌面版本和移动后备版本的视频 cms 脚本。用户代理由名为 detect.php 的文件检测,并将用户引导至该站点的桌面版本或移动版本。我创建了一个 webview 应用程序(android 和 ios),其中有广告,因此我希望将移动用户锁定在这些应用程序中,如果他们没有应用程序,则通过重定向他们安装我的应用程序来删除访问权限。我看到了一些建议,但他们没有考虑到混合网站,我担心我最终可能会将使用我的应用程序的人重定向到无限循环中来安装他们已经拥有的应用程序。下面我提供了我的 detect.php 代码...我还查看了谷歌分析并查看了 safari、safari-in-app、android 等。所以我不确定如何采取这种方法,因为涉及的许多因素。

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = trim(strtolower($useragent));
$new_URL = str_replace("www.", '', _URL);
$new_URL_MOBI = str_replace("www.", '', _URL_MOBI);
$current_URL = str_replace("www.", '', "http://".$_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
$current_URL_device = strpos($current_URL, $new_URL_MOBI);
$device_url = ($current_URL_device === false) ? 'desktop' : 'mobile';

if ( ! defined('COOKIE_DEVICE'))
{
    define('COOKIE_DEVICE', 'melody_device');
}

if(isset($_COOKIE[COOKIE_DEVICE]))
{
    if ($_COOKIE[COOKIE_DEVICE] != $device_url)
    {
        if ($device_url == 'mobile')
        {
            $takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
            header("Location: ".$takemehome."");
            exit;
        }
        elseif ($device_url == 'desktop')
        {
            $takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
            header("Location: ".$takemehome."");
            exit;
        }
    }
}
else
{
    if (strpos($useragent, "iphone") !== false || strpos($useragent, "symbianos") !== false || strpos($useragent, "ipad") !== false || strpos($useragent, "nook") !== false || strpos($useragent, "kindle") !== false || strpos($useragent, "IEMobile") !== false || strpos($useragent, "windows phone") !== false || strpos($useragent, "ipod") !== false || strpos($useragent, "android") !== false || strpos($useragent, "blackberry") !== false || strpos($useragent, "samsung") !== false || strpos($useragent, "nokia") !== false || strpos($useragent, "windows ce") !== false || strpos($useragent, "sonyericsson") !== false || strpos($useragent, "webos") !== false || strpos($useragent, "wap") !== false || strpos($useragent, "motor") !== false || strpos($useragent, "symbian") !== false || strpos($useragent, "android") !== false)
       {
          $device = 'mobile';
          setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);

          if (strpos($current_URL, $new_URL_MOBI) === false)
          {
             //User is NOT on mobile site, redirect
             $takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
             header("Location: ".$takemehome."");
             exit;
          }
          //Continue as usual
       }
       else
       {
          $device = 'desktop';
          setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);

          if (strpos($current_URL, $new_URL_MOBI) !== false)
          {
             //User IS on mobile site, redirect
             $takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
             header("Location: ".$takemehome."");
             exit;
          }
          //Continue as usual
       }
}
?>

【问题讨论】:

  • 您实际上没有提供任何相关代码。
  • 为什么不重写应用绕过detect.php页面?
  • @Bmargulies 你想要什么代码?
  • @nhgrif 很遗憾我不能这样做,因为我没有为 Windows 和黑莓用户提供应用程序。
  • 我添加该评论时根本没有代码。

标签: php android ios jquery-mobile mobile


【解决方案1】:

你不能。当用户从应用程序访问您的网站时,无论如何他都会使用设备的标准浏览器(在 iOS 中是 Safari)。因此,您可以知道这一点,只要应用程序是您的应用程序,您可以通过发送额外的 POST 或 GET 数据来开发 ad hoc > 发现访问是否来自您的应用。

一般来说,正如我所说,你不能。

【讨论】:

  • 谢谢,我想我将不得不使用弹出窗口询问他们是否正在使用该应用程序或想要安装它。
  • 是的,也许更好:) 请通过设置最喜欢的答案来关闭主题。谢谢。
  • 我刚刚读到我可以在我的应用程序中设置一个特定的用户代理,然后将其他所有内容定向到我的应用程序安装。
  • 是的,正如我上面所说,对于您的应用程序,您可以做任何您想做的事情。
猜你喜欢
  • 2010-09-12
  • 2015-06-07
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2018-06-16
  • 2012-07-22
相关资源
最近更新 更多