Welcome to WebmasterWorld Guest from 54.163.23.73

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP script does not print, no error

     

smallcompany

9:48 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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

10:29 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

11:31 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Thanks.

Your code looks better. Also thanks for pointing to missing parentheses.

rlange

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