0

I have been thinking from a long time that how can I edit the css files using php very safely and securely as we know css files are easily accessible and any one get the data using file_get_contents.

My question is can anyone edit the css file using file_get_contents?

Or is it safe to use while editing css file using this code?

$filename = "stylesheets/".$_POST['filename'];
$code = $_POST['code'];
if( file_exists($filename) ){
    file_put_contents( $filename, stripslashes($code) );
    echo 1;
}

I am just curious about it that I am using a very insecure way and anyone can access my css file and make changes even can destroy all my styling. I searching for the best and secure way to edit css files.

Please let me know if it is a possible duplicate or not a good question to ask, so I can close it my self and not by down votes.

Thank you.

1 Answer 1

1

This question doesn't really make much sense, but; how would anyone be able to edit the file? They'd need access to your server. If they used a script, it'd have to be on your server. I couldn't just make a random script run it on my PC or something and edit a file, it'd have to have direct access to the filesystem, and even then - the user PHP is running as would need to have write access to the file on top of that.

Is there a specific security concern you have? There doesn't seem to be a problem with what you're doing.

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

3 Comments

your answer does make sense. What should be the file permissions on the css files? Also another question to my mind : Can nobody access my stylesheet using this file_put_contents( 'url_to_css_file', stripslashes($code_css_here) ); ? or nobody can make cross domain edit requests to my server?
Like I said. The file could only be edited by PHP that is running on the same server with this method. And the user PHP is running as will need to have write permissions too. If you want to write to the file, you'll need to make sure that PHP has write permissions on the file. What that is will vary depending on your environment, and how you have your web server set up. Basically - this is definitely a non-issue for you. You seem to be quite new to this - honestly, you have nothing to worry about here.
Thank you very much for your explained answer. I was just worried that as I am accessing the css file anyone can access the files from their local machine and make changes to my file. Thank you :)

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.