php - 检查字符串和用户代理

标签 php query-string user-agent

我正在尝试设置一个 PHP 片段来检查是否满足 2 个条件,如果满足,它会回显一些文本。条件是:

  1. 查询字符串等于某个值。
  2. 浏览器是 Firefox。

它正在正确检查查询字符串,但它似乎不适用于浏览器(用户代理)。见下文:

<?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 '';
}

?> 

如有任何帮助,我们将不胜感激。

最佳答案

这就是我检查那些东西的方法:

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 '';
}

关于php - 检查字符串和用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127494/

相关文章:

c# - 检测 UserAgent 不是来自移动设备

email - 如何分辨Outlook 2003(或更早版本)的用户代理与IE open的区别?

php - 如何组合这些循环的 MySQL 查询?

php - 无法使用逗号将复选框值插入mysql数据库

asp.net - QueryString 为 null # 字符

jquery - 在按钮上单击重定向到 url,并从输入框中抓取查询字符串

javascript - 如何为使用 JS 创建的表单创建 CSRF 字段

PHP MySQL 计数和排序值

c# - 如何使用 ASP.NET Core 从查询字符串中读取值?

amazon-web-services - AWS CloudFront 如何检测移动设备