duanming0494 2011-12-20 10:34
浏览 50

为什么我的PHP IF / ELSE语句不能用于确定用户操作系统?

I'm trying to create a page that will redirect users depending on their operating system. To begin with I need it to work redirecting Windows XP users to place A, other Windows users to place B and Mac users to place C.

I used this to determine Windows/Mac which worked fine:

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];

if (strstr($useragent,'Win')) {
$os='<META HTTP-EQUIV="Refresh"
  CONTENT="1; URL=windows.php">';
} else {
$os='<META HTTP-EQUIV="Refresh"
  CONTENT="0; URL=mac.php">';
}

print "$os";
?>

Which worked fine, but then I tried to add a statement for Windows NT 5.1 (Windows XP)

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];

if (strstr($useragent,'Windows NT 5.1')) {
$os='<META HTTP-EQUIV="Refresh"
  CONTENT="1; URL=windows-xp.php">';
} else if (strstr($useragent,'Win')) {
$os='<META HTTP-EQUIV="Refresh"
  CONTENT="1; URL=windows.php">';
} else {
$os='<META HTTP-EQUIV="Refresh"
  CONTENT="0; URL=mac.php">';
}

print "$os";
?>

I'm not convinced I'm doing this the best way, but any help thrown at me would be much appreciated.

Thanks!

  • 写回答

3条回答 默认 最新

  • dongtong5242 2011-12-20 10:51
    关注

    Quoting my comment above:

    Relying on the User-Agent may have some legitimate uses, even if it is generally a bad idea. For example, displaying screenshots from the correct operating system on some kind of help / manual page. Or positioning the download link for the most appropriate version of the software first. Just make sure this is only for eye-candy, and there is a sane default option.

    For example, the WinXP-specific page should also include a link to the MacOS download, even if the Windows XP download is set in first place / most prominent. Also, the default version should also provide all download links, and should be fully operational. It can be, for example, equivalent to the page corresponding to the most-used version of your software.

    $browser = get_browser(null, true);
    switch ($browser->platform) {
    case 'WinXP':
        // XP-specific
        break;
    case 'WinVista':
        // Vista-specifict
        break;
    case 'MacOSX':
        // OSX default
        break;
    default:
        // Generic default
    }
    

    From the PHP documentation:

    In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

    browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.

    While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计