| PHP script does not print, no error
|
smallcompany

msg:4465141 | 9:48 pm on Jun 13, 2012 (gmt 0) | I have this script to detect if a browser is mobile:
<?php $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android"); $palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS"); $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry"); $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
if ($iphone || $android || $palmpre || $ipod || $berry == true) { print '<link href="mobile.css" rel="stylesheet" type="text/css" />'; else print '<link href="regular.css" rel="stylesheet" type="text/css" />'; /*header('Location: http://mobile.site.com/'); //OR echo "<script>window.location='http://mobile.site.com'</script>";*/ } ?> I commented out part of the original script and put my print in. Basically, I want to use different CSS file, depending on the browser. With this I get a blank page. When I check the source, the code is there only until the include PHP line. I have PHP reporting turned ON, but see no errors. Why it does not print? I have another PHP include command at the bottom of my template and it works fine. Thanks
|
g1smd

msg:4465160 | 10:29 pm on Jun 13, 2012 (gmt 0) | Add parentheses around the 5 ORd items. However, the code seems unecessarily complicated. I'd use:
if ( preg_match ('#(iP(hone|od)|Android|webOS|BlackBerry)#', $_SERVER['HTTP_USER_AGENT'] ) ) { do stuff } else { do other stuff }
|
smallcompany

msg:4465176 | 11:31 pm on Jun 13, 2012 (gmt 0) | Thanks. Your code looks better. Also thanks for pointing to missing parentheses.
|
rlange

msg:4465480 | 1:52 pm on Jun 14, 2012 (gmt 0) | For the learning experience, here's the problem: if ($iphone || $android || $palmpre || $ipod || $berry == true) { print '<link href="mobile.css" rel="stylesheet" type="text/css" />'; else print '<link href="regular.css" rel="stylesheet" type="text/css" />'; } |
| The curly braces aren't meant to encapsulate the entire if-then-else block; they're supposed to encapsulate each part separately. You essentially have a stray else (no matching if) inside of your if-then block, which I believe is a fatal error. if( something ) { // code here } else { // other code here } |
| -- Ryan
|
|
|