homepage Welcome to WebmasterWorld Guest from 54.227.21.95
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

    
PHP script does not print, no error
smallcompany

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4465139 posted 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

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



 
Msg#: 4465139 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4465139 posted 11:31 pm on Jun 13, 2012 (gmt 0)

Thanks.

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

rlange



 
Msg#: 4465139 posted 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

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