2

I know I can ask my php file to act like a css one, I already did. My problem is how to use variables in another css-php files ? My css.php file with global variables is loaded before others in html list, but it doesn't seem it includes it.

My html :

<link rel="stylesheet" type="text/css" href="assets/css/variables.css.php">
<link rel="stylesheet" type="text/css" href="assets/css/main.css.php">

My variables.css.php file :

 <?php
   header("Content-type: text/css");
   $color = "#004D7F";
 ?>

My main.css.php file :

<?php header("Content-type: text/css");  ?>
body {
    background: <?=$color;?>;
}

But it doesn't work, it doesn't recognize the variable $color. So I tried to add at main.css.php :

require("/assets/css/variables.css.php");

But it threw an error. Does someone know the answer ? How to get access php variables in another php file acting as css ?

Thank you in advance !

-- Carlos

4
  • Can you give us the error generated by the require ? please Commented Jul 13, 2015 at 22:21
  • I can't, because it works as a css file Commented Jul 13, 2015 at 22:49
  • Just few more questions. How did you launch the .html file ? Do you use apache (for example localhost/test.html) or directly launch the by double clicking on the file ? If you launch by double clicking, it can't work because your .php files can't be executed. Commented Jul 13, 2015 at 23:41
  • When i said apache i mean your local webserver sorry. Commented Jul 14, 2015 at 1:53

1 Answer 1

1

What the web server sees is not what the browser sees. Specifically, while the path to the file would be http://domain/assets/css/variables.css.php, on the server side you need to reflect the location the actual code. With the leading forward slash, the server is looking for the file starting with the root of the filesystem.

Remove the leading slash

That may be enough to make it work if the relative path is correct

If that doesn't help,

Remove parts of the path until it works

If both files are sitting in /var/www/html/assets/css/, then just doing require("variables.css.php") is enough.


Additionally, there's no need to include the variables.css.php inside the webpage. PHP will populate the values automatically inside main.css.php.

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

1 Comment

Thank you, you were right. I finaly just wrote require("variables.css.php") and remove the include in the html page and it works.

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.