Log in

View Full Version : 2 questions, 1 with a failed script. :p



NineTwoZero
01-06-2007, 03:00 PM
hi im new to php and most of the coding languages.. I tried to fix a script that would choose a stylesheet depending on the usersbrowser.. Unfortionatly it didnt work :p

Number 1.

<?php $style1
$style2
$browser = getenv("HTTP_USER_AGENT");

if $browser = mozilla;
<link href="style.css" rel="stylesheet" type="text/css" />;
else
<link href="style2.css" rel="stylesheet" type="text/css" />;
?>
am I totally outta bounce or is it possible?

and then I had anotheer question, is it possible to put a "noreload" on a menu, banner, logo and make my index div "like" a fram? http://underground.ninetwozero.com

peace - kalle

djr33
01-06-2007, 10:42 PM
the second question isn't related to php, so not sure on that, since php is my area, for the most part.


As for the code, there are several problems. I suppose that the line break after the if followed by a semicolon might work, but if you need multiple lines, so it like: if (...) {......}, surrounding the next few lines in brackets.
The main issue is that mozilla is not something standalone in PHP... it needs to be a string. So... 'mozilla', if nothing else. Here's a better way of trying it, though no browser detection is flawless.
$browser = strtolower($browser);
if (strpos($browser,'mozilla') !== FALSE) {
echo '<link href="style.css" rel="stylesheet" type="text/css" />';
}
else
echo '<link href="style2.css" rel="stylesheet" type="text/css" />';
}
<?php
//....
$browser = strtolower($browser); //convert the user agent string to lowercase
//now MOZILLA = Mozilla = mozilla... etc.
if (strpos($browser,'mozilla') !== FALSE) { //if 'mozilla' exists within $browser
//! means 'not', === means exactly equal to*, so !== means is not exactly equal to FALSE
//*== is equal to, but it sees 0, '' and FALSE as the same, so === seperates by type as well.
echo '<link href="style.css" rel="stylesheet" type="text/css" />'; //output style1
} //close if
else { //else with bracket
echo '<link href="style2.css" rel="stylesheet" type="text/css" />'; //output style2
} //close else
//....
?>
The text can't just sit there either... it needs to be output (echo function) and be inside quotes. (" is fine, but ' works, since you have double in the string. If you needed to use ", then you'd need to use, for example, href=\"style2.css\".... to escape the quotes in that.)

NineTwoZero
01-06-2007, 10:47 PM
holy sh*t... did you just solve my problem? :o ima try it out :D results back in a min.

djr33
01-06-2007, 10:51 PM
Note: I just edited the post a bit to make it clearer, with comments and such. Refresh and look at that if you haven't seen it.

NineTwoZero
01-06-2007, 10:58 PM
hmm it didnt quite work the way i thought :p but atleast its working.. now we needa know what the browsers called since mozilla seemed to be invalid..

http://underground.ninetwozero.com/test.php

NineTwoZero
01-06-2007, 10:59 PM
i got another idea.. cant we put like if browser is IE then a special style. else = FF etc = other style?

djr33
01-06-2007, 11:02 PM
Well, what I put SHOULD check if 'mozilla' exists anywhere within the string of the user agent. That seems logical to me.

If you want to see what the user agent string looks like, make a page with just this code:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

Also, I'd replace your user agent line in your PHP with:
$_SERVER['HTTP_USER_AGENT'];

$_SERVER is what I've used. getenv doesn't seem needed, though it might work as well, but I wouldn't know. It may also be outdated, and be the old method to do it, if you read that somewhere, or something.



Yes. Just copy/paste the syntax, like this:
if (....) {
...
}
else if (....) {
...
}
else {
...
}

(duplicate else if as much as needed)

Also, elseif is valid in php, but using else if is a good habit, since that's valid in other languages as well.

NineTwoZero
01-06-2007, 11:05 PM
ill try it later, gotta sleep now. thx, ill give u an update tomorrow :)

gnite.

NineTwoZero
01-07-2007, 12:19 PM
this is what i get with FF:

Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1

IE:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2)

so practiclly "MSIE" should be a good "searcher" for the string.. since it aint countin mozilla, jus pickin style2.css when sum1 enters.

NineTwoZero
01-07-2007, 01:00 PM
ok i tried to modify your script.. without success..
<?php

$browser = strtolower($browser);
if (strpos($browser,'MSIE') !== FALSE) {
echo '<link href="style2.css" rel="stylesheet" type="text/css" />';
}
else {
echo '<link href="style.css" rel="stylesheet" type="text/css" />';
}

?>

as u can see I changed the string to search from mozilla to MSIE. it didnt work tho. did I do sumthin wrong?

blm126
01-07-2007, 03:08 PM
Well, what I put SHOULD check if 'mozilla' exists anywhere within the string of the user agent. That seems logical to me.

:) I can tell you are new to browser detection... Check IE's user agent string
(http://en.wikipedia.org/wiki/User_agent)(near the bottom).


PHP is rather pointless here anyway if you just want to find IE try Conditional Comments



<!--[if IE]>
<link href="style.css" rel="stylesheet" type="text/css" />
<![endif]-->
<![if ! IE]>
<link href="style2.css" rel="stylesheet" type="text/css" />
<![endif]>


Just in the interest of learning,you would need to search for 'msie' NOT 'MSIE', NineTwoZero.

NineTwoZero
01-07-2007, 07:30 PM
ah thx blm for the post, sumthin odd happend before, I could see that the latest post came from u but i couldnt see ur post. (it was on the 2nd page.. lmfao)

will your script work? :D

edit: IT DID WORK. THX A LOT! :D THX EVERY1 :D

djr33
01-07-2007, 09:42 PM
yes, msie is lowercase, since the user agent string is converted to lowercase.

blm, what I did was intentional. The exact string can vary, and I'm assuming he wants any version of mozilla. Therefore, we KNOW that somewhere in the string, 'mozilla' will exist. So... I see absolutely no better way to check if the browser is mozilla.

As for conditional comments like that, note that he was checking for Mozilla, not for IE, at least in the first place.

NineTwoZero
01-08-2007, 02:57 PM
thats true.. can any1 check how the site looks in IE? looked good at home but not at school :S index goes lower than it should.

djr33
01-08-2007, 06:51 PM
Hmmm... looking at that link above to wikipedia, it appears that 'mozilla' appears in almost all user agent strings, including many non-mozilla browsers. Odd.

maybe searching for msie is better, as that appears to only be in ie browsers, though i'm not sure if it's in all. How annoying. Heh.

NineTwoZero
01-08-2007, 07:05 PM
djr33, is the index lower than the menu for u? (if ur usin IE) :S http://underground.ninetwozero.com

yoshi555
01-08-2007, 09:57 PM
wat does that mean like how your browser looks?

djr33
01-09-2007, 12:00 AM
I'm using safari at the moment. Looks fine here. I try to avoid IE :p
But I can check later if someone else doesn't first, when I'm on my PC.

NineTwoZero
01-09-2007, 04:59 PM
haha same here ;p nobodys checkin :(

djr33
01-09-2007, 10:08 PM
Yep. FF is fine. For IE (PC), it appears about 4 inches down the page, significantly under the menus.

blm126
01-11-2007, 01:21 AM
Hmmm... looking at that link above to wikipedia, it appears that 'mozilla' appears in almost all user agent strings, including many non-mozilla browsers. Odd.
That is what I was trying to point out... :). I think it is mostly historical reasons. Mozilla was the UA string of Netscape and it had new features. So browser vendors(like MS) added mozilla to their UA string. It's the same as Opera trying to spoof the IE user agent.

NineTwoZero
01-13-2007, 10:20 PM
got any solutions for the pushed down index? :(looks good here on my pc :S