5

Like everyone else I'm storing my site`s display information in style sheet files. And I want to create back-end cms so users will be able to, for example, change < h1 > color, size etc.

So, what's the right way to embed PHP code in my CSS file(s)?

Is adding this header:

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

And changing file extension in link:

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

Valid?

And what about JavaScript? At this moment I'm echoing <script type="text/javascript"> tags, but maybe there's also a way to embed everything in the .js files?

Thanks!

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
fomicz
  • 1,752
  • 7
  • 25
  • 33

3 Answers3

9

Yes, this is perfectly valid.

The same can be done for Javascript too by sending

<?php header("Content-type: application/javascript"); ?>

However, this is not optimal from a performance point of view because a PHP process has to be started for serving those resources.

If you have only very few dynamically changing CSS properties or JS variables, I would consider putting them into the document's head and continuing to serve the external files statically.

Remember that usually, there are no caching headers sent for PHP files. You'll have to take care of sending the correct headers inside your PHP script! Cheers @oracle certified professional for the reminder.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    but would your browser cache the style/javascript even if it is php served? –  Nov 24 '10 at 12:14
  • 1
    @oracle depends on the server settings for .php files, but usually no. You'd have to send cache headers along, good point, adding that to the answer. – Pekka Nov 24 '10 at 12:15
  • +1 Especially the static files with varying `` contents is worth considering. Also, make sure to set cache headers (e.g., `Expires` and `Last-Modified`) properly if serving semi-static files via PHP. – jensgram Nov 24 '10 at 12:15
  • 2
    ... and in walks Captain Slow :) – jensgram Nov 24 '10 at 12:15
  • Ok guys, so hov to send cache headers for my new pseudo-css and pseudo-js files? Actually after changing my stylesheet extension to .php everything still works smooth. – fomicz Nov 24 '10 at 12:20
  • @fomicz caching is definitely a good idea. See http://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php – Pekka Nov 24 '10 at 12:21
  • @Pekka, the correct MIME type for JavaScript is `application/javascript`, not `text/javascript`: http://annevankesteren.nl/2006/05/javascript-mime-type – mercator Nov 24 '10 at 12:31
  • @Pekka, well, there are a ton of different MIME types being used for JS (http://krijnhoetmer.nl/stuff/javascript/mime-types/), and only `text/javascript` works reliably in the `script` tag's `type` attribute, but then you don't need to use that attribute anyway. – mercator Nov 24 '10 at 12:40
  • Guys, I've tried with .js and it doesn't work for me, at least with .js files. for css is fine... – fomicz Nov 25 '10 at 15:34
5

What you're doing is absolutely valid.

However if you're running a bigger site with many visitors, you should concern to just let PHP "build" a "real" CSS file when your user updates his or her design to save your servers performance to better needed things:

<?php

header("Content-type: text/css");

// Your database magic here to fetch the user-specific designs 

// open the cached css
$cachefile = "cachedCSS/mycss.css";

if (file_exists($cachefile)) {
    // the page has been cached from an earlier request
    // output the contents of the cache file
    include($cachefile); 
    // exit the script, so that the rest isnt executed
    exit;
}

$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush(); 

Read more about it here: http://www.theukwebdesigncompany.com/articles/php-caching.php

Industrial
  • 41,400
  • 69
  • 194
  • 289
  • +1, this is the best of both worlds (although also the most complex to implement, usually worth the effort only for high-traffic sites) – Pekka Nov 24 '10 at 12:28
0

Make sure you are parsing the php in those files.

In .htaccess :

AddType application/x-httpd-php .php .css .js

This will ensure that any <?php ?> tags in file types other than .php will be parsed by the server and the php code isn't readable by users.

Greg
  • 21,235
  • 17
  • 84
  • 107
  • 3
    Awww... If you do this, every request for a CSS / JS resource will be passed through PHP's interpreter. As a general rule, that is not good. Better use the native .php extension in that case – Pekka Nov 24 '10 at 12:14