PDA

View Full Version : Using PHP to import browser specific PHP



sirbrent
12-07-2007, 11:30 PM
errr, the subject was meant to be: Using PHP to import browser specific CSS

Hello, I am trying to write a PHP script that will import a specific css stylesheet depending on browser type.

it am using an old script i had which told you if your browser version was out of date, but im trying to modify it for this purpose.

here is a sample of the the page running the script



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("bcscript.php"); ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Browser Check Tester</title>
</head>

<body>
</body>
</html>


and the script code ("bcscript.php" in the above example) i am trying to modify is this:



<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) {
echo "string for IE goes here";
} else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') ) {
echo "string for Safari goes here";
} else {
echo "string for all browsers except safari and ie goes here";
}
?>


so basically i want to load a specific css for ie and safari and then a default one for all other browsers. i know there are several ways to do this, but this is the method i want to use for my purpose.

so how can i insert something like this into the echo string for the PHP?

<link href="ieonly.css" rel="stylesheet" type="text/css" />

thanks for any help.

shadmego
12-08-2007, 01:16 AM
Something like this maybe:




<?php
// bcscript.php

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) {
$css = "ieonly.css";
} else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') ) {
$css = "safonly.css";
} else {
$css = "general.css";
}
?>


Then in your header:



<head>
<?php include("bcscript.php"); ?>
<link href="<?php echo $css ?>" rel="stylesheet" type="text/css" />


I'm not a coder, so I built a test page ... http://www.4christministry.org/php/test.php which holds the html code. Test it in a few different browsers. I tested in IE7, Firefox 2 and Safari (for Windows) and it worked. There might be a way to clean this up a bit too, but someone (they know who they are ;) ) can help you more with that!

~regards

sirbrent
12-08-2007, 01:30 AM
Thank you for your response. Your method is just a little bit cleaner than mine. Thank you.

Hmm, What Im really trying to accomplish here is having a php script that detects what browser you are using and if its NOT firefox or opera to create a little overlayed (semi-transparent) div at the top of the page that says something like "(insert company/site name here) is a strong supporter of web standards and recommends the Firefox Web Browser. Click here to learn about how this affects you."

It is meant to be a not too distracting or in-your-face way of pushing firefox. If a user has firefox or opera it wont display this message.

Im not too smart with PHP though (but am learning)... and I try to avoid JS whenever possible.

shadmego
12-08-2007, 01:50 AM
Greetings ... I'm up much later than I should be. I'm studying for an RHCE practice test ... some would say it's a RHCE prep class final. I can tell you iptables SUCKS!!!

... I digress ...

You should be able to accomplish this easily, the noob way which is what I would do, by creating a simple image with the text you want to say. Then call that image with CSS into the div you want to create for those nefarious people still using IE*, which the exception of yours truly! :D

Anyway. My way is simple, but not at all elegant, and probably not what you are looking for. The only other way my simple mind that think to do it is make another script *sort of* like this untested bit of code:

In test.php


<head>
<?php include("bcscript.php"); ?>
<link href="<?php echo $css ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<?php

if ($css == 'ieonly.css') { ?>

<h1>Get A Clue IE USER!!! GET FIREFOX!!! OR SAFARI!!! OR LYNX!!!! tee-hee ...</h1>
<?php } else { ?>
<h1>Whew! I'm glad to be working with someone that has a clue!</h1>
<? }; ?>
</body>
</html>


Again, better test that out to see if it works ... I might have just enbarassed myself.

~regards

sirbrent
12-08-2007, 02:00 AM
haha i like it. especially the "tee-hee"... i might use that. although codewise i am looking for something more "elegant".

good luck with your practice test.