简体   繁体   中英

Why won't my PHP IF/ELSE statement work for determining the users operating system?

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!

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.

You can loop through this array of user agents and matching operating systems and then redirect users according to result.

    $OSList = array(
    // Match user agent string with operating systems
    'Windows 3.11' => 'Win16',
    'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
    'Windows 98' => '(Windows 98)|(Win98)',
    'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
    'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
    'Windows Server 2003' => '(Windows NT 5.2)',
    'Windows Vista' => '(Windows NT 6.0)',
    'Windows 7' => '(Windows NT 7.0)',
    'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
    'Windows ME' => 'Windows ME',
    'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
    );

A couple of things, you're using strstr() when a simple strpos will do. I would stick it in a simple switch statement too so you can easily set a default/match multiple conditions.

Secondly, the proper way to redirect in PHP is to use the header() command like so:

header("Location: windows.php");

Put it all together, you get this:

$useragent = $_SERVER['HTTP_USER_AGENT'];

switch(true) {

    // Windows XP
    case stripos($useragent, 'Windows NT 5') !== false: 
        header("Location: windowsxp.php");
        break;

    case stripos($useragent, 'Windows ') !== false: // Windows whatever
        header("Location: windows.php");
        break;

    case stripos($useragent, 'mac') !== false:
    default :
        // Default, maybe do nothing?

}

Read up on PHP switch statements and hopefully you will understand the layout. As others have said, relying on useragent isn't perfect, but for your simple download case it should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM