检查字符串和用户代理 [英] Check String and User Agent
本文介绍了检查字符串和用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试设置一个PHP代码段,该代码段检查是否满足2个条件,如果满足,它将回显一些文本.条件是:
I am trying to setup a PHP snippet that checks if 2 conditions are met and if they are, it echoes some text. The conditions are:
- 查询字符串等于某个值.
- 该浏览器是Firefox.
它正在正确检查查询字符串,但它似乎不适用于浏览器(用户代理).见下文:
It's checking the query string properly but, it doesnt seem to be working for the browser (user agent). See below:
<?php
function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = '';
if(preg_match('/Firefox/i',$u_agent))
{
$ub = "firefox";
}
else
{
$ub = "other";
}
}
if (isset($_GET['print']) && $_GET['print'] != "" && $ub = 'firefox')
{
$pg = $_GET['print'];
if (!file_exists('1'))
{
echo '<b>It worked!</b>';
}
}
else
{
echo '';
}
?>
任何帮助将不胜感激.
推荐答案
这就是我要检查的东西:
This is what i do to check out that stuff:
if(strlen(strstr($_SERVER['HTTP_USER_AGENT'],"Firefox")) <= 0 ){ // if not firefox
//do something
}
并添加到您的代码中:
function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = '';
if(strlen(strstr($u_agent,"Firefox")) > 0 ){
$ub = 'firefox';
}
else {
$ub = 'other';
}
return $ub;
}
if (isset($_GET['print']) && $_GET['print'] != ""
&& get_user_browser() == 'firefox')
{
$pg = $_GET['print'];
if (!file_exists('1'))
{
echo '<b>It worked!</b>';
}
}
else
{
echo '';
}
这篇关于检查字符串和用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文