如何检测用户是否正在使用应用程序或浏览器访问我的网站?

时间:2014-01-14 22:33:15

标签: php android ios jquery-mobile mobile

大家好我正在使用视频cms脚本,它有桌面版和移动版后备版。用户代理由名为detect.php的文件检测,并将用户定向到桌面版本或站点的移动版本。我创建了一个webview应用程序(android和ios),我有广告,所以我想让移动用户锁定这些应用程序,如果他们没有应用程序通过重定向安装我的应用程序删除访问。我已经看到了一些建议,但他们没有考虑混合网站,我担心我最终可能会将使用我的应用程序的人重定向到一个无限循环来安装他们已经拥有的应用程序。下面我提供了我的detect.php代码......我还看了一下google analytics并看了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
       }
}
?>

1 个答案:

答案 0 :(得分:0)

你不能。当用户从应用访问您的网站时,他在任何情况下都使用设备的标准浏览器(在iOS中是Safari)。所以,您可以通过发送额外的POST或GET数据,只要该应用是您的应用,您就可以开发 ad hoc >发现访问是否来自您的应用。

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