0

I am trying to process some CSS using PHP. The CSS I want to process is page-specific and so I want to set a variable in my index.php to only echo the CSS when the variable is set.

index.php

<?php
    $name = 'index'; 
?>
<?php
    include 'inc/header.php';
?>

header.php

<head>
      <meta charset="utf-8">
      <title></title>       
<?php include 'import.php'; ?>
</head>

import.php

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

The header is properly set for this file. and the CSS is interpreted.

styles.css.php

.jumbotron {
    background-image: url(../img/jumbotron.jpg);
    <?php 
        if ($name === "index") { 
            echo("height: 50VH;"); /* Does not echo anything*/
        }
        var_dump($name); // Evaluates to NULL
    ?>
}

How can I make sure $name is set to it's value, as set in index.php?


EDIT: My final solution was to make an object from the stdClass and add the variables I needed as attributes to the object. putting a GET request in the link to the CSS file which was the Base64 of the JSON string of the object.

  • 1
    try with global $name – Strahinja Djurić May 10 '16 at 21:50
  • 2
    did you include the index.php to styles.css.php? – Lorence Hernandez May 10 '16 at 21:52
  • http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not – ScaisEdge May 10 '16 at 21:52
  • 2
    That's because `$name` is not defined in styles.css.php. You should also enable error reporting because this should have produced a warning. – Mike May 10 '16 at 21:52
  • @LorenceHernandez That fixed it, but now there is a complete PHP file present in my CSS file. And that's not really what I want. –  May 10 '16 at 21:56
  • @StrahinjaDjurić I have used `$GLOBALS['name'] = $name` in `index.php` but that did not fix it. the `styles.css.php` still dumps `NULL` when I var_dump `$GLOBALS['name']`. –  May 10 '16 at 21:59
  • @MegaXLR You really seem to have no clue what's going on here. I suggest you read the http://php.net/manual/en/function.include.php manual page. – Mike May 10 '16 at 21:59

4 Answers4

2

The <link> tag makes an HTTP request for a file totally independent of the PHP pages that are including each other, so $name is not available in styles.css.php.

To do it this way you need to link the style sheet something like this to pass $name as a get variable:

<link rel="stylesheet" href="css/styles.css.php?name=<?php echo $name; ?>" type="text/css" />

Then in the style sheet styles.css.php use $_GET['name']:

    if ($_GET['name'] === "index") { 
        echo("height: 50VH;");
    }
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

The problem is that you are linking to that styles.css.php from inside the rendered html. This means that page will be fetched in a separate request (as you can se when you inspect the page). This request never passes trough your index.php, and so the $name variable will not be set.

I would advise you to include the css in the rendered HTML inside a style block. It should be as easy as changing your import.php to this:

<style>
<?php include 'css/styles.css.php' ?>
</style>

This also has the added benefit of reducing the number of requests the browser has to make, and should therefore speed up the page.

This is btw not a very standard method of using css. I believe it would be better to add some sort of id (or data attribute) on your body tag that indicates the name of the page you are on. In the css file (that does not run trough php, just a standard css file), you could then do something like this:

.jumbotron {
    background-image: url(../img/jumbotron.jpg);
}
#index .jumbotron {
    height: 50vh;
}
Pevara
  • 14,242
  • 1
  • 34
  • 47
0

The file styles.css.php does not know about the $name variable - that's why it is null when doing the var_dump($name). With your <link> you just output the content of your style.css.php as plain html/css. You need to do either an include_once('styles.css.php') or a require_once('styles.css.php') so that you can properly evaluate the $name variable.

codedge
  • 4,754
  • 2
  • 22
  • 38
0

You are having an HTML page generated by index.php. This tells the browser to load the external style sheet at the line <link rel="stylesheet" href="css/styles.css.php" type="text/css" />. Loading external data by the browser is a completely new request. Thus the PHP file generating the style sheet does not know anything about the variables declared in the script serving the HTML page.

Actually the variable $name is unset. Since it is given as argument by reference to var_dump (the builtin function is declared to take arguments by reference), the reference results in null.

If you want to distinguish between different style sheets in only one PHP script, append a GET parameter to the URL as query:

<link rel="stylesheet" href="css/styles.css.php?name=<?PHP echo $name;?>" type="text/css" />

Take that parameter in syles.css.php:

<?php $name = isset($_GET['name']) ? $_GET['name'] : ''; ?>
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42