0

Hi I need to include a php code to a folder in one of my css file. This is how I have link my images and other things together and it works.

For example for image I normally do this:

<img src="<?php $directories->fetchdir($img); ?> MYIMAGE.png" /> 

This will general turn out to be www.example.com/html/img/MYIMAGE.png

directories <--- this is my class
fetchdir <-- this is my function 

I have try the same thing in my .css file and its not working. I have try the same method in my test.css file. below is what I have try

@font-face {
    font-family:'FontAwesome';
    src:url('<?php $directories->fetchdir($fonts);?>fontawesome-webfont.eot?v=4.0.3');
}

Please help. Thanks!

8
  • You cannot mix php in css file Commented Feb 23, 2014 at 21:41
  • You can generate a css file from php though Commented Feb 23, 2014 at 21:41
  • @Fabio Well you can if you tell your webserver to associate ".css" with php execution, it's just generally not a great idea. Commented Feb 23, 2014 at 21:41
  • 2
    If the CSS depends on your PHP, you should put it in the <head> of the document. Commented Feb 23, 2014 at 21:42
  • 2
    Then there's bound to be a better approach to what you're trying to achieve. Precompilation/caching comes to mind. Commented Feb 23, 2014 at 21:44

2 Answers 2

2

The answer will depend on the server your running. Are you using Apache? (Chances are that you are.)

The issue is that Apache doesn't know that you want to process CSS files as PHP. You can add this line to your .htaccess file make it.

AddType application/x-httpd-php .css

Note: This is probably not a good idea. The more script you process, the more time it'll take to serve your user their content and the more chances there are for security holes. As mentioned above, you can simply include CSS code in the head of your document with <style> tags (the PHP will process properly there).

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @TravisGD thanks for the answer. Its a one page site and its use alot jquery to make it looks like it has alot of pages. nothing i sgetting sent to the server but i have a contact from.
Another solution that may work well for you is formatting your css files as foo/bar.css.php and adding header("Content-type: text/css"); to your file.
in my bar.css.php add header("Content-type: text/css"); it inside the php tag?
in my head tag i am calling bar.css.php like this <link rel="stylesheet" href="<?php $directories->fetchdir($css); ?>/font-awesome.css.php">
Unless fetchdir prints out the directory, you probably want to replace that with either echo $directories->fetchdir($css); or change the PHP tags to <?= and ?>, which print out the result of a single expression.
0

You may use URL rewriting so you can forward your css requests to a php file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.