Asked  8 Months ago    Answers:  5   Viewed   34 times

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:

  1. That the query string equals a certain value.
  2. That the browser is 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 '';
}

?> 

Any help would be appreciated.

 Answers

26

This is what i do to check out that stuff:

if(strlen(strstr($_SERVER['HTTP_USER_AGENT'],"Firefox")) <= 0 ){ // if not firefox

  //do something

}

And added into your code:

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 '';
}
Wednesday, March 31, 2021
 
Joegramming
answered 8 Months ago
71
  1. How do I determine a mobile browser

Here's some javascript that will do the job: http://www.quirksmode.org/js/detect.html

  1. Is it bad practice to have the mobile site on a different domain or subdomain?

It's good practice, and a popular convention is to use http://m.yoursite.com for mobile if your main url is http://yoursite.com

Noah

Wednesday, March 31, 2021
 
Sethunath
answered 8 Months ago
26

As per the comments, I still have no idea why you're using basename, however, using set_locale will work in PHP 5.2+:

<?php
    function qs_search($qs) {
        $query_string = array();
        $query_string['search'] = basename($qs);
        return http_build_query($query_string);
    }
    setlocale(LC_ALL, "en_US.UTF8");
    var_dump(qs_search("regular"));
    var_dump(qs_search("???????????"));
?>

As stated, this will NOT work on PHP 5.0.0 - 5.1.6. Click to see results.

Edit:

Apparently the developers of Drupal had the same problem and created their own basename function because of this bug, and that should fix your issue on PHP 5.0.0 - 5.1.6.

Saturday, May 29, 2021
 
Naveen
answered 5 Months ago
69

After much research, I figured it out. There is a way to set a user agent for Android WebView.

webview.getSettings().setUserAgentString("user-agent-string");

http://developer.android.com/reference/android/webkit/WebSettings.html

Wednesday, June 16, 2021
 
Marcelo
answered 5 Months ago
92

I think it is tricky to get the full OS name and full browser name since many browsers identify themselves differently. You will probably need some fancy regex and then it might not even work 100% of the time.

Here is the simple method I use to identify the browser. You may be able to adapt it to suit your needs.

<?php

public static function getUserAgent()
{
    static $agent = null;

    if ( empty($agent) ) {
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if ( stripos($agent, 'Firefox') !== false ) {
            $agent = 'firefox';
        } elseif ( stripos($agent, 'MSIE') !== false ) {
            $agent = 'ie';
        } elseif ( stripos($agent, 'iPad') !== false ) {
            $agent = 'ipad';
        } elseif ( stripos($agent, 'Android') !== false ) {
            $agent = 'android';
        } elseif ( stripos($agent, 'Chrome') !== false ) {
            $agent = 'chrome';
        } elseif ( stripos($agent, 'Safari') !== false ) {
            $agent = 'safari';
        } elseif ( stripos($agent, 'AIR') !== false ) {
            $agent = 'air';
        } elseif ( stripos($agent, 'Fluid') !== false ) {
            $agent = 'fluid';
        }

    }

    return $agent;
}
Tuesday, August 17, 2021
 
Blundell
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share