Include: CSS with .PHP File Extension

Include: CSS with .php file extension?

gnarf nailed it.

I do:

<link rel="stylesheet" type="text/css" media="screen" href="<? echo TMPL ?>css/name-of-file.css.php">

and then in top of your .css.php file:

<?
header('Content-Type: text/css');
// print out your php-driven css...
?>

Include PHP inside CSS file without PHP extension

This is just for apache2 and php? Then one idea would be to interpret the .css as a .php which can be done by editing the .conf that includes the php extension or find this line somewhere in the config files:

AddType application/x-httpd-php .php

and edit to show

AddType application/x-httpd-php .php .css .phtml .etc .etc

How can a php include access the css files

You have correctly identified the problem, that the HTML cannot find the CSS. That is directly because of this tag:

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

Specifically, this part of the tag:

href="styles.css"

You have told your code to look for the styles.css file in the same directory as the index.php file. Is that correct?

Usually, the web site structure looks like this:

public_html
- css
- js
- includes
- img

Your website seems to be structured like this:

includes
public_html

Imagine yourself inside the index.php file. All files that are INCLUDEd become part of index.php, as if they were there originally. So, you are expecting to find the CSS file here (as per your <link rel="stylesheet" tag):

public_html/style.css

If they are really inside a CSS folder, then perhaps this will fix it:

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

Update:

No need to echo out your HTML in PHP, you can literally just do this:

header.php

<div class ="header">
<h1>The Best Sport</h1>
<h1 class="sitetitle">AllWaterPolo</h1>
<img src ="img/51wmckj8p1l__sx300__1.png" class="wpball" alt="Water Polo Ball" />
<h2 class="homeScreenLink"> <a href ="index.html">Water Polo!</a></h2>
</div>

If the other include files are similar, then it appears your rendered file will all be HTML. Therefore, the next step is: where is your style sheet? Try this. In the address bar of your browser, type:

localhost/styles.css

And see if your stylesheet appears. If not, try this:

localhost/css/styles.css

If the second one works, then change this:

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

to this:

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

Note: I might not have your path structure correct. Amend as required.

How can I include CSS in php?

As long as you set your MIME type in style.php to CSS, you should be in business. Add this to the very top:

<?php Header ("Content-type: text/css; charset=utf-8");?> 

Another option if you're running on an Apache server is to tell it to check .css files for PHP. Add this to your .htaccess file to do this:

AddType application/x-httpd-php .css 

Then you could simply include a regular .css file:

<link rel="stylesheet" href="http://www.mydomain.com/css/style.css">


Related Topics



Leave a reply



Submit