View Full Version : A shorter IF statement
wkdown
09-06-2007, 08:56 PM
Hello all, looking for the shorter version of an IF statement. I am formatting a CGI script based on the user's browser. The obvious IF statement is:
if ( ($ENV['HTTP_USER_AGENT'] =~ 'Mozilla') || ($ENV['HTTP_USER_AGENT'] =~ 'MSIE') || ...
Is there a way to make it more concise? Say like:
if ( $ENV['HTTP_USER_AGENT'] =~ ('Mozilla','MSIE','Safari','Opera') ) {
... or something to this effect?
brad211987
09-06-2007, 09:19 PM
I'm not that familiar with Perl/CGI but can you create a list holding the different browsers you are checking for, and then do a check to see if '$ENV['HTTP_USER_AGENT']' is in the list? This would clean the code up, I know how to do it in ruby but not perl so much.
wkdown
09-06-2007, 10:01 PM
I'm guessing you mean put them into an array and loop through, right?
my @browsers = [ 'Mozilla', 'MSIE', 'Safari', 'Opera' ];
for ($foo=0; $foo<@browsers; $foo++) {
next unless ($ENV{HTTP_USER_AGENT} =~ $browsers[$foo]);
$mobile=1;
}
That makes the issue moot since this is even longer. I want to keep it to just the IF statement.
wkdown
09-06-2007, 10:11 PM
Actually, I figured it out using regular expressions. For anyone who cares to know...
if ($ENV{HTTP_USER_AGENT} !~ /Mozilla|MSIE|Safari|Opera/ ) { $mobileUser = 1 }
The pipes | are regular expression ORs, so it will look for equivalence with any of the four.
FishMonger
09-06-2007, 10:14 PM
There are several approaches that can be taken, but you didn't provide enough info on exactly what you want to accomplish so I can't say which of the approaches will best suite your needs.
See if this what you're after.
if( $ENV{'HTTP_USER_AGENT'} =~ /(Mozilla|MSIE|Safari|Opera)/ )
FishMonger
09-06-2007, 10:16 PM
Good, you figured it out. I guess I should always refresh before posting a solution.
wkdown
09-06-2007, 10:48 PM
Yes I got it, thanks alot though.
Note to anyone who Googles this: This is not an end-all solution for mobile browsers. These are only the four major browsers on PCs which account for ~95% of browsers used to view pages. Two exceptions I've found already (which may be major down the road)...
1. Apple iPhone and iPod Touch users; their browsers use a form of Safari
2. PSP users; PSP uses a form of Mozilla
Blackberrys (the most common handheld browsers) use their own proprietary browser. Example USER AGENT data:
BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/104
FishMonger
09-06-2007, 11:42 PM
Another thing to keep in mind is that the HTTP_USER_AGENT var can be spoofed to be anything the user wants, which means you don't want to rely on its value. I could access your site and set my HTTP_USER_AGENT to Fishmonger.
http://search.cpan.org/~gaas/libwww-perl-5.808/lib/LWP/UserAgent.pm
$ua->agent( $product_id )
Get/set the product token that is used to identify the user agent on the network. The agent value is sent as the "User-Agent" header in the requests. The default is the string returned by the _agent() method (see below).
If the $product_id ends with space then the _agent() string is appended to it.
The user agent string should be one or more simple product identifiers with an optional version number separated by the "/" character. Examples are:
$ua->agent('Checkbot/0.4 ' . $ua->_agent);
$ua->agent('Checkbot/0.4 '); # same as above
$ua->agent('Mozilla/5.0');
$ua->agent(""); # don't identify
wkdown
09-07-2007, 04:06 PM
Thats true but I'm guessing in most applications that doesn't matter. In my case, I am presenting a table of data pulled from a database and said data is editable. Now a 12 column table is fine on a PC but horrendous on a BlackBerry. So I weed out if they are on a handheld and, if so, simplify the table. If someone wants their Blackberry to feign being Firefox, be my guest and enjoy the horizontal scrolling! haha
crmpicco
09-08-2007, 02:06 AM
it's $ENV{REMOTE_ADDR}, don't think it would work with $ENV[REMOTE_ADDR]. No need for quotes either.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.