Forum Moderators: coopster
added: AddType application/x-httpd-php .css to my .htaccess
in my css file I have:
<?php
header('content-type:text/css');
/* caches css file */
header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT");
$self=$_SERVER['PHP_SELF'];
/* Colour schemes */
if (preg_match('index.php',$self))
{
$bg='background: #000 url(bg1.jpg) top left no-repeat;';
}
else if (preg_match('photos.php',$self))
{
$bg='background: #000 url(bg2.jpg) top left no-repeat;';
}
?>
body {
margin: 0;
padding: 0;
color: #fff;
font: 11px tahoma;
<?php echo $bg;?>
}
Is it possible?
[edited by: SuzyUK at 10:01 am (utc) on Mar. 24, 2008]
[edit reason] thread split and move [/edit]
a better method might be to put this in the header:
<?php
// gets current page name
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
And under that, I would need "if current page is equal to index.php, then echo this stylesheet, else echo stylesheet2.
?>
Any advice?
I need to echo a stylesheet. Would something like this work?
<?php if($_GET['page_id']==index.php){echo '<link rel="stylesheet" type="text/css" href="default.css" />'}else{echo '<link rel="stylesheet" type="text/css" href="otherstyle.css" />'} ?>
<?php
$page_id=27;
if ($page_id==27){ echo '<link rel="alternate stylesheet" type="text/css" media="screen" title="sunset" href="style1.css" />'; }else{echo '<link rel="alternate stylesheet" type="text/css" media="screen" title="sunset" href="style2.css" />'; }; ?>
HTML:
<link rel="stylesheet" type="text/css" href="mycss.php">
mycss.php
<?php
header('Content-Type: text/css');
?>
/* The rest of your CSS... */
That should be all that's required in order to get PHP in your CSS file.
IMHO however, I would have thought this was best done in CSS alone - no PHP required. Simply add a unique page ID to the body element and CSS accordingly. You can then allow the CSS to cache, reducing bandwidth and improving speed.
HTML:
<body id="homepage">
CSS:
body#homepage {
background-image: url(bg1.jpg);
}
body#contactpage {
background-image: url(bg2.jpg);
} Having an ID on the body element allows you to provide a lot more page specific styling as well.