Browser selection with php

Hi everybody.

I am trying to establish a simple way to insert a module for Internet Explorer 8 and other one for the rest of browsers.

I have read all the solutions in
http://stackoverflow.com/questions/5302302/php-if-internet-explorer-6-7-8-or-9

And set this script:

  <?php
    $br = strtolower($_SERVER['HTTP_USER_AGENT']);
    if(ereg("msie 8", $br)) {echo 'Module for Internet Explorer 8';} 
    else {echo 'Module for the rest of browsers";}
   ?> 

It works perfectly, but I am not a php expert and perhaps it is not the best solution among all of the mentioned in Stackoverflow.

I would appreciate any comment.

Thanks very much.

Kinda need to know what this is for. Is this to change CSS, which would be one of the reasons you would detect IE 8 or below.

Adding onto the above post…if this is to coverup something that you feel isn’t doable in IE8 and below (or whatever it is) please let us know. We have experts in this forum who can tell you if you actually need this functionality

You got it.

The idea is to include a module just for Internet Explorer 8, and I would like to know whether the script above is the most adequate or not.

Well why do you need a module for IE8? What makes that browser so special as to get the module but others don’t? Is this an alternate module since you find IE8 doesn’t support something?

Yes, that is exactly the issue. The module works normally but in IE8, so I have included the script to add a variation (without jQuery) to have it working properly in this browser.

And it works. The problem is whether this is the best way to do it.

Thanks.

That depends on what the problem is for IE8.

You might be able to use client-side testing
http://api.jquery.com/jQuery.support/

though that page recommends using

an external library such as Modernizr instead of dependency on properties in jQuery.support.

Forget about jQuery please.

I am talking about the browser detection code:

<?php
    $br = strtolower($_SERVER['HTTP_USER_AGENT']);
    if(ereg("msie 8", $br)) {echo 'Module for Internet Explorer 8';} 
    else {echo 'Module for the rest of browsers";}
   ?>

Is it Ok? Should I use other script?

Thanks.

A working script that works as you want:

<?php

$browser = strtolower($_SERVER['HTTP_USER_AGENT']);

$detect = strpos($browser, 'msie 8.0');

if($detect) {
    echo 'Found';
} else {
    echo 'Not Found';
}

Maybe.

If the user sends the HTTP header and isn’t spoofing it.
And
if the string doesn’t begin with “m”

http://php.net/manual/en/function.strpos.php

Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

I see, thank you.

Sorry about the delay in replying this post.

If Pocsan’s script is not recommended, what script should I use?

What about my original proposal?

    <?php
    $br = strtolower($_SERVER['HTTP_USER_AGENT']);
    if(ereg("msie 8", $br)) {echo 'Module for Internet Explorer 8';} 
    else {echo 'Module for the rest of browsers";}
   ?>

Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.