【问题标题】:How to change web content based on operating system [duplicate]如何根据操作系统更改网页内容[重复]
【发布时间】:2017-04-09 10:08:24
【问题描述】:

我正在为移动应用程序设计一个 wordpress 网站。 对于访问者,我想显示基于操作系统的文本、图像,如下页 -

https://www.sapphireone.com/accounting-software/accounts/
在此页面中,在“帐户结构部分”中,有一个图像会根据操作系统(即 mac 和 windows)而变化。

我想为 android 和 iPhone 做类似的事情。我正在使用以下代码,但它不起作用。

//function.php 的代码

function find_andoird() {
$ua = $_SERVER[‘HTTPS_USER_AGENT’];

 /*    ====    Detect the OS    ====    */

 // Android
    $android        = strpos($ua, 'Android') ? true : false;

 // iPhone
   $iphone        = strpos($ua, 'iPhone') ? true : false;

  return $android;

 }

//模板文件代码

if(find_andoird() == true) { 

<p><strong>Android View</strong></p>
<p>
<img src=“accounting-android.png”  style="float:left;width:400px;height:600px;">
The android view is displayed here.  
</p>
}  

else { 

<p><strong>iPhone View</strong></p>
<p>
<img src=“accounting-iphone.png” style="float:left;width:400px;height:600px;">
The iphone view is displayed here.  
</p>
}

}

我能得到一些帮助吗?谢谢

【问题讨论】:

  • 更改函数以返回 $ua 值,并将其输出以查看函数正在处理的字符串。
  • 首先是HTTP_USER_AGENT,它周围的引号是错误的,应该是简单的引号
  • 嗨,bansi,我已更改为 $ua = $_SERVER['HTTP_USER_AGENT'];还是不行
  • 嗨炫耀者,你能帮帮我吗,怎么做..因为它是在wordpress中,所以我有点困惑。

标签: php html wordpress


【解决方案1】:

这个功能可能会有所帮助。

<?php

$user_agent     =   $_SERVER['HTTP_USER_AGENT'];

function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

function getBrowser() {

    global $user_agent;

    $browser        =   "Unknown Browser";

    $browser_array  =   array(
                            '/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $browser    =   $value;
        }

    }

    return $browser;

}


$user_os        =   getOS();
$user_browser   =   getBrowser();

$device_details =   "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>

【讨论】:

  • 我还没有弄清楚,如何在 wordpress 中做,但它在普通的 php 脚本中运行良好。可能是我在 function.php 文件或模板文件中做错了什么。
【解决方案2】:

有一个名为detectizr 的modernizr 扩展(https://cdnjs.com/libraries/detectizr,您必须将modernizr 放在首位)。它会给你标记操作系统,版本,浏览器,设备......然后你可以使用css显示例如:

/* Hide image with class-a by default */
img.class-a {
    display: block;
}

/* showing it in windows 10 only */
.windows.windows10 img.class-a {
    display: block;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2023-03-27
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多