Jump to content

[SOLVED] Newbie needs assistance with simple browser/platform detect script...


Jeniveve

Recommended Posts

Hi, My name is Jen and I am trying to write what I would assume to be a simple script to detect 2 values in an http_user_agent string and if those 2 values exist to send the users browsers to another page... I have run into some issues an have not been able to solve them after searching on the web and looking through the php books I have...

 

I am hoping someone here might be able to assist me in a judgement free way.  I am not interested in people telling me what I am doing is 'not right', I am ot interested in people telling me that if my pages cannot render in all browsers then I am doing something wrong and I am not interested in hearing that I should use css hacks instead. 

 

What I am interested in is why my script doesn't work and what about it makes it so.  I am also interested in why I have been unable, after writing and rewriting this scrip in many different ways, to figure out how to compare the http_user_agent information to 2 values, and if those values exist write a simple echo statement...

 

Can anyone out there give me a hint or out and out right nonjudgemental assitance as to why the way I have written the below script renders errors and will not work?

 

Thank you very much in advance to anyone who is willing to help a newbie find her way.

 

Jennifer

 

<?php
if (preg_match("/MSIE/i" && "Win/i", "$_SERVER[HTTP_USER_AGENT]")) {
	echo '<link rel="stylesheet" type="text/css" href="win_styles.css">';
		}	else if (preg_match("/Firefox/i" && "/Win/i", "$_SERVER[HTTP_USER_AGENT]")) {
				echo '<link rel="stylesheet" type="text/css" href="styles.css">';
					} 	else	{
							echo '<link rel="stylesheet" type="text/css" href="styles.css">';
									}

?>

Guest prozente

1. Using && doesn't work there as you'd think

2. You didn't enclose HTTP_USER_AGENT in quotes so it is looked at as a constant

3. Since you are using the same for FF and other it's better to just remove the else if

 

 
<?php
if (preg_match('/MSIE(?:.*)Win/i', $_SERVER['HTTP_USER_AGENT'])) {
  echo '<link rel="stylesheet" type="text/css" href="win_styles.css">';
}else{
  echo '<link rel="stylesheet" type="text/css" href="styles.css">';
}
?>

 

you could of also used HTML conditional comments

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

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

 

Thanks to the two people who answered my questions, I appreciate it a lot!

 

To Prozente: your examples helped me a lot, thank you very much.  I am playing with the conditional comments as well.  I do have one question however, it's about dropping the elseif, I have to add 2 more different conditions that have to be matched...I also need to look for and separate out Safari for the Mac and FF for the Mac... I thought that I *had* to use elseif when I am listing more than one condition.. Is this correct?  Or, could I just keep writing else statements until all matches are found?

 

To boo_lolly thank you for the hudzilla page but it is not doing what I need specifically and doesn't really address the issue I am trying to deal with, which has to do with how to list 2 expressions and match them to one string when the string is a global variable...

 

Also the php.net page manual, I have read that, especially the page you refer to hwoever I find it incredibly hard to understand their writing and that is why I posted my question here.

 

However, thank you for your input, I appreciate it.

 

Jennifer

Guest prozente

I took away the elseif because you were echoing the same stylesheet

 

With the other browser requirements you'd do something like this

<?php

if (preg_match('/MSIE(?:.*)Win/i', $_SERVER['HTTP_USER_AGENT'])) {

    echo 'IE stylesheet';

} elseif (preg_match('/Macintosh(?:.*)Safari/i', $_SERVER['HTTP_USER_AGENT'])) {

    echo 'Safari stylesheet..';

} elseif (preg_match('/Macintosh(?:.*)Firefox/i', $_SERVER['HTTP_USER_AGENT'])) {

    echo 'Mac FF stylesheet..';

} elseif (preg_match('/Windows(?:.*)Firefox/i', $_SERVER['HTTP_USER_AGENT'])) {

    echo 'Win FF stylesheet..';

} else {

    echo 'Other stylesheet..';

}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.