Mecha CMS

Code snippet collection for developers.

PHP in CSS and JavaScript Files

Author: Taufik Nurrohman · 1194 Views


Using HTACCESS to parse PHP syntax in CSS and JavaScript files.

This snippet can be used for any other PHP CMS with .htaccess support. Open any .htaccess file that you see in the asset folder, then add this command to the next line:

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

You can now embed PHP syntax on any CSS and JavaScript files stored in the asset folder that stores this custom .htaccess file. Don’t forget to tell the proper mime content type to header: text/css for CSS files and application/javascript for JavaScript files:

<?php header('Content-Type: text/css'); ?>
body {background:#<?php echo substr(md5($_SERVER['REQUEST_TIME']), 0, 6); ?>}
<?php header('Content-Type: application/javascript'); ?>
window.$host = '<?php echo $_SERVER['HTTP_HOST']; ?>';


No comments yet.