Tuesday, March 20, 2012

PHP, change the theme of a website



The website theme is defined in theme.css. I want administrator can change the them after login the website. I copy theme.css to theme_template.css. and replace
 width:100%;
 to
 width: main_width;    
The following code reads  theme_template.css file,  changes main_width  according to the  input value from the form, then writes the output to theme.css. After you submit the form, the website them is changed.


<form method="post" action="<?php echo $PHP_SELF;?>">
main_width:<input type="text" size="12" maxlength="12" name="main_width"  >:<br />
page_width:<input type="text" size="12" maxlength="36" name="page_width">:<br />
  <input type="submit" name="submit" value="Submit Form"><br>
</form>

<?php
$inputfile = $_SERVER['DOCUMENT_ROOT'].'/_css/theme_template.css';
$outputfile = $_SERVER['DOCUMENT_ROOT'].'/_css/theme.css';
if (isset($_POST['submit'])) {
  $main_width = $_POST['main_width'];
  $file_contents = file_get_contents($inputfile);
  $file_contents = str_replace("main_width",$main_width,$file_contents);
  file_put_contents($outputfile,$file_contents);
 }
?>

No comments:

Post a Comment