Search code examples
phphtmlcssstylesheettemporary-files

Is it safe to let PHP generate my stylesheet with file_put_contents?


Trying to create maintainable websites, I thought it would be nice to call in lines of html with php and generate a stylesheet of only the needed css. This way I can edit css once, for multiple targets. I managed it this way:

This is the PHP calling in the needed element:

<?php
    $id = 'checkbox-1';
    $label = 'Agreement';
    include 'blocks/forms/checkbox-1.php';
?>

Then this is the checkbox-1.php file, calling in the html as asked:

<input type="checkbox" class="<?php echo $class; ?>" id="<?php echo $id; ?>">
<label for="<?php echo $id; ?>"><span></span><?php echo $label; ?></label>

This is the same php file, writing the needed css to the stylesheet if hasn't been done before:

<?php
    if (strpos($blocksCss,'/* ----- CHECKBOX 1 ----- */') == false) {
        $myString = '
        /* ----- CHECKBOX 1 ----- */
        css goes here';
        file_put_contents($blocksCss, $myString, FILE_APPEND);
    }
?>

But I guess this causes problems with security and with multiple requests as well, because this is affecting the stylesheet on the server that needs to be writable and more than one person is trying to reach. Am I right or is this a safe way?

If not, would it be possible to do it another way? Creating a new stylesheet per user? Temporarily?

Thanks in advance!


Solution

  • This solution is not good... And that's why:

    1. As I can see, You are modyfiyng the same CSS file. This will affect all the users that are wathing the site.

    2. file_put_contents with multiple refreshes sometimes can erase the whole file (like in most text based visits counters).

    3. CSS files are not that big, while modyfing them, force the browser to not cache them, but download the full file each time, as it was modified. In case of perormance it is easier to download one big file (20kb?) Then multiple times download 19.5kb file.

    4. Javascript and Jquery should do the work, if you want to dynamically change some styles. Why not to use that technique?

    5. file_put_contents is one of the functions, that we should... Not use, if we mustn't. There are always few things (permissions, reload problems, multi request problems, lock problems on file) that can go wrong.

    6. if (strpos($blocksCss,'/* ----- CHECKBOX 1 ----- */') == false) This is also a very time consuming operation on big files...


    Last hope solution:

    You can create the session for each user, and make a CSS file for each user (session_id.css). IN that case, the css file will be cached, but this is also NOT A GOOD idea, if there are many users. This is rather a dead-end-solution, or must-finish-project-in-a-minute solution :).


    Jody Fitzpatrick - great idea of generating the file on the fly, without using file put contents. This is the best solution in php in my opinion. But still Jquery or javascript should be better. As generating the whole CSS on the fly is server time consuming.

    Hope it helps.