homepage Welcome to WebmasterWorld Guest from 54.234.7.161
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Browser filtering with PHP. Is it possible?
AlexB77

5+ Year Member



 
Msg#: 4396178 posted 11:22 pm on Dec 9, 2011 (gmt 0)

Is this actually possible to filter out content by browser type?

For instance, I have some content, such as Facebook Like button, G+ and Twitter buttons, that I would like not to be shown to the visitor if visited through Firefox.

All of the scripts for all buttons are currently placed in the single social.php, and being passed as php include.

So the question is, if this is possible to let the include file run on the server, when user viewing site with any browser but Firefox?

Thanks for your help in advance

 

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 5:00 pm on Dec 10, 2011 (gmt 0)

You can check the $_SERVER['HTTP_USER_AGENT'] superglobal - which (in theory) uniquely identifies a browser, browser version and OS.

My user agent string for Firefox 8 on Win Vista is being reported as:
Mozilla/5.0 (Windows NT 6.0; rv:8.0) Gecko/20100101 Firefox/8.0


However, it is unreliable, as browsers can be configured to send anything and even pretend to be other browsers by sending fake user agent strings. Also, the practise of sending different content to different browsers based purely on the browser (as opposed to features the browser supports) is very much frowned upon.

Curious as to why you would want to hide your social gadgets from Firefox users?

abushahin

5+ Year Member



 
Msg#: 4396178 posted 3:34 pm on Dec 23, 2011 (gmt 0)

one thing you could try which wont filter firefox but other than internet explorer is have a div in which your share buttons are and hide it in the css.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 9:03 pm on Dec 23, 2011 (gmt 0)

one thing you could try which wont filter firefox but other than internet explorer is have a div in which your share buttons are and hide it in the css.


Are you referring to conditional comments [webmasterworld.com]? These can be used to target IE (or not-IE), so not firefox specifically (like you say), but will be dropped in IE10.

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 12:01 pm on Dec 27, 2011 (gmt 0)

Hey Guys,

here is what I came up with:

<?php
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"Firefox");
if ($browser == true) { echo ' '; }
else
include("../includes/social-middle_templates.php");?>


the .php file in my includes folder contains code for all my social buttons such as G+, Facebook like and Twitter.

Now the reason why I need to hide it from Firefox users:

Unless I am mistaken, most of tools used by G. to measure the page load time are directly depending on firefox browser therefor, if your page load was 3.5s after using the script above will be something like 1.4 - 1.7s.

I do not really mind hiding my buttons from firefox users simply because my stats show that only 7% of my users use firefox and in fact most of the users do not even care about clicking on those buttons anyway.

See for your self, would be nice to hear back from you.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 12:47 pm on Dec 27, 2011 (gmt 0)

Unless I am mistaken, most of tools used by G. to measure the page load time are directly depending on firefox browser therefor, if your page load was 3.5s after using the script above will be something like 1.4 - 1.7s.


AFAIK the Google page load time stats are gathered from users that have the Google Toolbar installed with the PageRank feature enabled, which could include Internet Explorer and Firefox users.

$browser = strpos($_SERVER['HTTP_USER_AGENT'],"Firefox"); 
if ($browser == true) { echo ' '; }


Just a point, this check will fail if "Firefox" ever appeared at the start of the user agent string (although it shouldn't under normal circumstances). It would perhaps be more correct to write:
if ($browser !== false) { echo ' '; }

Solution1

10+ Year Member



 
Msg#: 4396178 posted 6:01 pm on Dec 28, 2011 (gmt 0)

The above code will also fail if the useragent header is not set. What I would do:


if (!empty($_SERVER['HTTP_USER_AGENT']))
{
$isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'],"Firefox") !== false);
} else {
$isFirefox = false;
}
if ($isFirefox) { echo ' '; }

incrediBILL

WebmasterWorld Administrator incredibill us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 7:39 pm on Dec 28, 2011 (gmt 0)

Instead of inline code, I use the following 2 functions to make my code more bulletproof when handing CGI variables.

I never have to remember to code for whether the variables are missing or not because it's always automatic, no exceptions, nada.

$ua=get_server('HTTP_USER_AGENT');
$isFirefox = ( $ua!=false && strpos($ua,"Firefox")!=false );


# get $_SERVER variables
# protected against throwing warnings
function get_server($var) {
$value=false;
if( isset( $_SERVER[$var] ) )
$value=$_SERVER[$var];
return($value);
}

# get parameters passed by POST or GET
# protected against throwing warnings
function get_parm($var) {
$value=false;
if( isset( $_REQUEST[$var] ) )
$value=$_REQUEST[$var];
return($value);
}

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 12:14 pm on Dec 30, 2011 (gmt 0)

Isn't it secure enough when used inline?

@incrediBILL
How to implement the code you have shared?

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4396178 posted 1:33 pm on Dec 30, 2011 (gmt 0)

>> Isn't it secure enough when used inline?

What he has suggested is not so much about security as much as good coding practice. Whenever you see yourself repeating a bunch of code over and over again, it is probably best to create a function that does this for you.

In this case, the code he doesn't want to keep repeating is a check to see if the variable "isset" before fetching the value.

And if you like pretty functions, this could be rewritten to not use a placeholder ;)


function get_parm($var) {
return isset($_REQUEST[$var]) ? $_REQUEST[$var] : false;
}

incrediBILL

WebmasterWorld Administrator incredibill us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 3:53 pm on Dec 30, 2011 (gmt 0)

In this case, the code he doesn't want to keep repeating is a check to see if the variable "isset" before fetching the value.


It also protects against forgetfulness to do the "isset" check and laziness to skip "isset" when hacking code together, so it's d) all of the above ;)

And if you like pretty functions, this could be rewritten to not use a placeholder


True, but I broke it down for WebmasterWorld so it was readable for the novice as tertiary operators aren't as easy to understand for beginning programmers IMO.

ymmv

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 2:09 am on Jan 3, 2012 (gmt 0)

@ Solution 1

the script you suggested does the job, but other way around, meaning that instead of hiding in Firefox, it shows in it and hides in any other browser.

Can you please help to correct it?

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 12:13 pm on Jan 3, 2012 (gmt 0)

Solution1's logic looks OK. $isFirefox is true when the firefox browser is detected, false otherwise (ignoring any anomalies with the user agent string).

However, there is still the question as to whether this is really the correct thing to do. As I mention above, the page load times that Google records could be more dependent on IE users than Firefox, particularly since you say you don't get many Firefox users.

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 12:13 am on Jan 4, 2012 (gmt 0)

I understand how the script works, but question is whare to put
include("../includes/social-middle_templates.php");
in the instance where Firefox browser is not detected?
AlexB77

5+ Year Member



 
Msg#: 4396178 posted 1:53 am on Jan 4, 2012 (gmt 0)

Would that be correct?
<?php
if (!empty($_SERVER['HTTP_USER_AGENT']))
{
$isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'],"Firefox") !== false);
} else {
$isFirefox = false;
}
if ($isFirefox) { echo ' '; }
else
include("../includes/social-middle_templates.php");?>

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 12:57 pm on Jan 8, 2012 (gmt 0)

Hey guys, I am back again.

Here is what I have as an outcome:

The code that you have suggested works fine, and I have used it like i have mentioned in my previous post, but I have some errors in the error log such as

PHP Notice: Undefined index: HTTP_USER_AGENT in /var/www/vhosts/example.com/httpdocs/index.html on line 36


the line 36 is
if (!empty($_SERVER['HTTP_USER_AGENT']))


I do not know what I am doing wrong or what would I need to do to correct an error.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 3:09 pm on Jan 8, 2012 (gmt 0)

Hhhmm?! I don't think that line should produce that PHP Notice?! What version of PHP are you using? empty() should simply return true if the variable passed to it is undefined - and it does on my test server with full error_reporting() enabled.

You could change the logic and use this instead...
if (isset($_SERVER['HTTP_USER_AGENT']))


(But this shouldn't make any difference, apart from perhaps being easier to read.)

AlexB77

5+ Year Member



 
Msg#: 4396178 posted 3:43 pm on Jan 8, 2012 (gmt 0)

I am using php5. As I have said it works and does what it is intended to do but this error keeps coming up and, I do not know if you have noticed, only on one single page of the site.

Thanks for you advise, I will have a look at it and report back if more problems will appear.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4396178 posted 4:13 pm on Jan 8, 2012 (gmt 0)

...this error keeps coming up and, I do not know if you have noticed, only on one single page of the site.


This isn't actually an error, it is an advisory notice. And yes, it should still work (in this instance). But you shouldn't even be getting this notice IMO.

Is this code on several different pages of your site but only on 1 page does it produce this notice? What is different about this 1 page? Do you always get this notice, even when you yourself visit the page? Or only for some unknown visitors?

The superglobal $_SERVER['HTTP_USER_AGENT'] might be empty (or not even set - as the notice indicates) for only a very small percentage of visitors. Which is the reason for this empty() check.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved