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

    
Creating a dynamic css file
too much information

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 7:41 pm on Dec 7, 2010 (gmt 0)

I'm not even sure if this is possible with PHP but I am trying to create a dynamic css file and I'm having trouble finding a way to determine which page is loading the css file.

The problem comes in because the css file is loaded with the <link> tag so it's not "included" and technically it doesn't have a referer so I'm having a heck of a time trying to get this sucker to work.

Here's what I have tried within my css file:


if ($_SERVER["REQUEST_URI"] == "/index.php" || $_SERVER["REQUEST_URI"] == "/") {
echo "background-image: url(images/body.jpg);\n";
} else if ($_SERVER["PHP_SELF"] == "/index.php" || $_SERVER["PHP_SELF"] == "/") {
echo "background-image: url(images/body.jpg);\n";
} else if ($_SERVER["HTTP_REFERER"] == "/index.php" || $_SERVER["HTTP_REFERER"] == "/") {
echo "background-image: url(images/body.jpg);\n";
} else {
echo "background-image: url(images/body2.jpg);\n";
};

The real pain of this experiment is that I can't just echo out the server calls to see what the result is because it doesn't display in the page source because of the <link> tag. (Not to mention that if I load it directly it would give me different results.)

So is there a way to do this within the css file itself? I know I can do it in the call to the css file but I'd rather try to get this method to work if it is at all possible.

 

topr8

WebmasterWorld Senior Member topr8 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4239826 posted 8:00 pm on Dec 7, 2010 (gmt 0)

you can't run php code in the css file

unless you have set up apache to parse css as php of course.

personally i use 'dynamic' css a lot - and i serve the standard css file then in the head of the page using php i do something similar to what you did, this then overwrites (has higher precedence) the css file.

surely there is a firefox extension that lets you view the 'assets' of a page, eg. external style sheets, images etcetc

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 8:51 pm on Dec 7, 2010 (gmt 0)

You could also add a query string parameter in the <link> tag to tell your PHP/CSS script where the request is coming from.

too much information

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 1:04 am on Dec 8, 2010 (gmt 0)

Sure you can run PHP in the CSS file.

Just add the correct header at the top of the page

<?php
header('Content-type: text/css');
?>

And my css file works great, I'm just having trouble building all of the dynamics into the css page itself because of the way the files are embedded in the html.

I was thinking of the query string parameter but that's too simple. There has to be a way to detect the page that is calling the css page. I'm just wondering if anyone might have another idea of how to go about it.

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4239826 posted 12:08 pm on Dec 8, 2010 (gmt 0)

Well if you want dynamically generate a css just set it up in the head section of the html like a regular php script. For example:

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

When the browser access it it will be executed like a regular php file and you output the CSS. Setup the cache headers like HTTP_IF_MODIFIED_SINCE so the style script is cached (apart of the content type you already have).

The style.php can do whatever you want, access the database store information about the access in general whatever a regular php script does and you can pass parameters with the link.

too much information

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 2:28 pm on Dec 8, 2010 (gmt 0)

Right, that's what I've got. What I'm trying to do now is find a way to detect which page is loading the css file.

Usually you can use $_SERVER["PHP_SELF"] to determine which page is loading your include file, but in this case the css file is loaded with the <link> tag.

So my question is how do I find out within the css.php file which of my pages is pulling the css using the <link> tag?

I've tried : ($_SERVER["REQUEST_URI"], $_SERVER["PHP_SELF"], $_SERVER["HTTP_REFERER"], $_SERVER["SCRIPT_NAME"] but none of these seem to work. It looks like the <link> tag causes the css.php file to load independently of whatever page is using the data.

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 9:53 pm on Dec 8, 2010 (gmt 0)

How are you dealing with browser caching? Isn't it true that if you use the same URL for the CSS/PHP script on all of your pages, then you might not see any call at all if the user has recently visited another page on your site?

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4239826 posted 10:41 pm on Dec 8, 2010 (gmt 0)

What I'm trying to do now is find a way to detect which page is loading the css file.

You pass a parameter to the style.php which is loaded by the file in question like:

<link rel="stylesheet" type="text/css" href="style.php?id=1" />

where id is the parameter and 1 could imply index.php

How are you dealing with browser caching

You can setup the cache headers with HTTP_IF_MODIFIED_SINCE, the expires and cache-control

too much information

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4239826 posted 5:32 pm on Dec 9, 2010 (gmt 0)

I was running into the cache issue and I don't really want to stop the css file from being cached so I ended up going with the querystring option which works like a charm.

I was just trying to see if I could find a way to do it with all of the dynamic code within the css file itself. I think what I learned is that the <link> tag makes that impossible, but it does make it easier for the browser to know that using the cached version is ok.

So it was a fun experiment and I learned something but in the end I don't think it's possible to detect where the call is coming from within the css file itself.

Thanks for all of the input!

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4239826 posted 5:49 pm on Dec 9, 2010 (gmt 0)

The browser will open a separate connection to get the style file. So you cannot directly communicate with the script that included the style. But using the parameter is an easy way to distinguish between scripts.

I also experimented with storing in the database some value from the script accessed and then check it in the style file. Depends what you want to do, I found it useful for some statistics. But I did not find it useful to distinguish between visitors and bots.

The extra parameter with the link tag is the easiest and most effective though.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4239826 posted 5:58 pm on Dec 9, 2010 (gmt 0)

Wouldn't this be easier?

CSS:
body { background-image: url(/images/body2.jpg); }
body.home-bg { background-image: url(/images/body.jpg); }

At the head of index.php:

$home = 1;

Standard for all PHP scripts:

include ($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");

then in header.php,
<link rel="stylesheet" type="text/css" href="one-stylesheet-to-rule-them-all.css">
<body<?php if ($home) { echo ' class="home-bg"'; } ?>>

topr8

WebmasterWorld Senior Member topr8 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4239826 posted 11:34 am on Dec 13, 2010 (gmt 0)

>>Sure you can run PHP in the CSS file.

yes, i know that, i do it myself with a dynamic robots.txt - as long as you parse it as php on the server.

however IMHO why would you want to?

the point of an external css file is so that the browser caches the file and reuses it without having to download it again, by changing it dynamically you lose that benefit.

rocknbil's post makes sense and in fact is what i said right at the beginning of the thread, except he put it more eloquently!

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