473,378 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Is it good practice to detect browsers like this..?

106 100+
We all know that browser compatibility is almost always an issue. What I'd like to know is if this way of telling which browser the user is using is a good practice.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $who = strtolower($_SERVER['HTTP_USER_AGENT']);
  3.  
  4. if(preg_match("/msie/", $who)) { 
  5.     //code for IE 
  6. }
  7. elseif(preg_match("/mozilla/", $who)) { 
  8.        //code for Firefox
  9. }
  10. else { 
  11.     //code for Other Browsers
  12. }
  13. ?>
  14.  
Thank you in advance.
Aug 3 '10 #1

✓ answered by Markus

You might manipulate your user-agent header to gain access to areas that only permit certain user-agents to view content.

By the way, using preg_match to match simple strings is wasteful. Use strpos instead.

9 1359
dlite922
1,584 Expert 1GB
try this:

http://api.jquery.com/jQuery.browser/



Dan
Aug 3 '10 #2
londres9b
106 100+
It's interesting but if the user doesn't have javascript ..:(
So you don't recommend using the php code above?
Aug 3 '10 #3
TheServant
1,168 Expert 1GB
If it works, it works. I suggest you do some thorough testing though. But yeah, it looks OK to me. Probably even worth looking at the jQuery Browser code dlite922 posted to see how they do it and if you can improve yours at all.
Aug 3 '10 #4
Dormilich
8,658 Expert Mod 8TB
as mentioned in dlite992’s link, you should prefer feature detection over browser detection. the user agent string can be manipulated easily, after all.
Aug 3 '10 #5
TheServant
1,168 Expert 1GB
I haven't really thought about this too much, but why would anyone want to manipulate their agent string? If all it does is choose what content is served, there is not really a security risk is there? But, I think that feature detection is simpler and a better choice. Do you know how it handles IE "having" some features, but not really "having" them?
Aug 3 '10 #6
zorgi
431 Expert 256MB
I like to use conditional comments for detection of IE browsers.
Aug 4 '10 #7
Markus
6,050 Expert 4TB
You might manipulate your user-agent header to gain access to areas that only permit certain user-agents to view content.

By the way, using preg_match to match simple strings is wasteful. Use strpos instead.
Aug 4 '10 #8
londres9b
106 100+
Thank you all
Aug 9 '10 #9
use php function get_broswer();

But it's not advisable and not a good practice to write browser dependable code.
Aug 26 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Erik De Keyser | last post by:
Hi group, I have a couple of projects with up to 8 forms. On most projects I write the code on each form with no modules. The last project I use 1 module and minimal code on each form , in fact...
13
by: Mel | last post by:
CAN YOU suggest an alternative ? ..heading, heading-R, heading-K, heading-G, heading-W { font-family: arial,helvetica, ms sans serif, sans-serif; font-size: 10pt; font-style: normal;...
7
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int...
3
by: Andy Dingley | last post by:
We've all seen this structure many times: <ul> <li><a href="..." >Click here</a></li> <li><a href="..." >Click here</a></li> </ul> Now it's obvious good practice to have sensible link texts,...
3
by: Andyza | last post by:
In my Global.asa file I have the following 2 subs for opening and closing my db connection: Sub OpenConn() Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open...
1
by: wxqun | last post by:
Our company is now trying to make a "standard" of creating a base view for each user table. This initiative is suggested as a good practice by a data modeling consultant who is helping us to build...
66
by: KimmoA | last post by:
Hey! Some questions about C that have been bugging me for a while... 1) Is inline a valid C keyword or not? I was kind of surprised of not finding it in C, to be honest. My "The C Programming...
6
by: MLH | last post by:
I have frmMainMenu with the following two code lines invoked on a button click... 2420 DoCmd.OpenForm "frmVehicleEntryForm", A_NORMAL, , , A_ADD, A_NORMAL 2440 DoCmd.GoToRecord , , A_NEWREC ...
1
by: dan | last post by:
Hi, I think I need a gridview, formview, another gridview, and another formview on a single page. There is a parent-child relationship between the first gridview and the formview and then...
1
by: Gieke | last post by:
hi group, I'm new here. Worked a bit with xml over the years, and now having a discussion if something is allowed or not, or good practice or not. It's about repeating a tag within a tag that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.