What is the correct way to embed PHP code in my CSS and JavaScript files?

Like everyone else, I save information about my site in style files. And I want to create back-end cms so that users can, for example, change <h1> color, size, etc.

So what is the right way to embed PHP code in my CSS files?

Adds this header:

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

And change the file extension in the link:

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

Really?

What about javascript? I am repeating tags at the moment <script type="text/javascript">, but maybe there is a way to insert everything into .js files?

Thank!

+5
source share
3 answers

Yes, that’s absolutely true.

The same can be done for Javascript by sending

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

, PHP .

CSS JS-, , .

, , PHP. PHP script! Cheers @oracle .

+9

, , .

, , , PHP "" "" CSS , , :

<?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(); 

: http://www.theukwebdesigncompany.com/articles/php-caching.php

+5

, php .

.htaccess:

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

, <?php ?> , .php, , php- .

0

All Articles