Jump to content

Edit a CSS File Through PHP


QuackMasterDan

Recommended Posts

Hi there,

I'm creating a site for a client which is almost finished. As a final feature he has requested the ability to change CSS attributes through a simple interface in a webpage (he is not familiar with coding, so it must be easy to use)

 

I have a css file named css.css and lets say it looks something like the following:

 

html, body
{height: 100%; margin: 0; padding: 0;}

.picturetitlefont
{color:#fff; font-family: courier;}

.picturedescriptionfont
{
color#000; font-family: sans-serif;}
}

 

 

I need to create a dropdown menu for him to select a font style he desires (like font color or family) and a submit button that will write that style into the css.css file and save it. No individual user styles, just one solid file for the whole site. Essentially an php script that when submitted opens css.css, and is able to replace the content of one of these classes. (So one dropdown menu will replace the {} of .picturetitlefont)

 

Any ideas/solutions?

Link to comment
Share on other sites

You build your CSS file using PHP by reading the choices from the drop down menus, and making the string that will hold the CSS. You can then use a combination of fopen, fwrite and fclose to open (or create) a file, write the CSS string to it, and close it.

Link to comment
Share on other sites

Excellent, with that idea I've created a cheaters solution for the problem.

 

instead of css.css, it is now css.php. This php file just echoes the entirety of the old css file, yet at the end I added two includes. One include for the picturetitlefont, and another include for the picturedescription font. My dropdown/submit forms just write over the entirety of those included files, and to the page viewer, it looks like one nice css file.

 

Thank you for the quick reply.

Link to comment
Share on other sites

I may have a better suggestion, simply make, lets say styles.php, and link to it, from the main page, like you would link a normal .css file. Then include the below in your main/index.

 

<?php
session_start();

if ((isset($_GET['StyleSheet'])) && (preg_match("/^[\w\s]{3,24}$/D", $_GET['StyleSheet']))) {
$_SESSION['styl'] = $_GET['StyleSheet'];
} else { $_SESSION['styl'] = 'Helvetica'; } // Default Font
?>

 

Styles.php:

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

session_start();
?>
body {
  font-family: <?php $_SESSION['styl'] ?>;
}

 

You can then link to the different styles using a simple link.

<a href="?StyleSheet=monospace">monospace</a>
<a href="?StyleSheet=serif">serif</a>

 

The above is based on my PHP StyleSheet Selector

 

 

The "w" in the regexp means, Any word character (a-z 0-9 _), and the "s" means, Whitespace (space, tab CRLF). This was just written off-hand from some short reference, you may want to only allow the space. Check http://www.regular-expressions.info/ for some tutorials on this.

 

I wouldn't want to change a .css file from an interface, i'd rather save the css in my database in that case, and thats where the "Content-type: text/css" comes in, actually you can create some amazing applications that way.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.