Search code examples
phpcssisset

Link for CSS.php?


So lately I've realized I want to make my sites as simple as possible in certain ways, one of them is having as few files as possible. I've looked into linking for CSS-PHP files using query strings, similar to how XenForo does it.

<link rel="stylesheet" href="css.php?css=xenforo,form,public">

Now linking for a file is easy, but how would I add the CSS inside a PHP file without using the style tag? Can I just do something like the block below?

<?php
    if(isset($_GET['form'])) {
?>
CSS HERE
<?php
    }
?>

Solution

  • Add

    <?php
    header(Content-type: text/css");
    ?>
    

    to the top of your php stylesheet. eg:

    <?php
    header("Content-type: text/css");
    ?>
    h1 {
        color: blue;
    }
    

    <html>
    <head>
        <title></title>
        <link rel="stylesheet" href="test.php">
    </head>
    <body>
        <h1>test</h1>
    </body>
    </html>