Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created April 16, 2011 15:47
Show Gist options
  • Select an option

  • Save callumacrae/923215 to your computer and use it in GitHub Desktop.

Select an option

Save callumacrae/923215 to your computer and use it in GitHub Desktop.
<?php
// Check if the cookie is already set, otherwise set some default styling options
// Also, we need to unserialize the value of the cookie as we are working with arrays
$style = !empty($_COOKIE['style']) ? unserialize($_COOKIE['style']) : null;
// First check if the keys in the arrays are empty, then check if it starts with #
$body_background_color = !empty($style['body_background_color']) ? (strpos($style['body_background_color'],"#") !== 0 ? "#". $style['body_background_color'] : $style['body_background_color']) : "#fff";
$body_text_color = !empty($style['body_text_color']) ? (strpos($style['body_text_color'],"#") !== 0 ? "#". $style['body_text_color'] : $style['body_text_color']) : "#000";
$body_text_size = !empty($style['body_text_size']) ? $style['body_text_size'] : 12;
$example_background_color = !empty($style['example_background_color']) ? (strpos($style['example_background_color'],"#") !== 0 ? "#". $style['example_background_color'] : $style['example_background_color']) : "#fff";
$example_text_color = !empty($style['example_text_color']) ? (strpos($style['example_text_color'],"#") !== 0 ? "#". $style['example_text_color'] : $style['example_text_color']) : "#000";
$example_text_size = !empty($style['example_text_size']) ? $style['example_text_size'] : 10;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="style.php" rel="stylesheet" type="text/css" />
<title>Modify stlye</title>
</head>
<body>
<?php
// If the form hasn't been submitted we should show the form to the user
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="submit">
<fieldset>
<label for="background">Backgorund color</label><br />
<input type="text" id="background" name="body_background_color" value="<?php echo $body_background_color ?>" /><br />
<label for="textcolor">Text color</label><br />
<input type="text" id="textcolor" name="body_text_color" value="<?php echo $body_text_color ?>"/><br />
<label for="textsize">Text size</label><br />
<input type="text" id="textsize" name="body_text_size" value="<?php echo $body_text_size ?>"/><br /><br />
<label for="examplebackground">#example background color</label><br />
<input type="text" id="examplebackground" name="example_background_color" value="<?php echo $example_background_color ?>"/><br />
<label for="exampletextcolor">#example text color</label><br />
<input type="text" id="exampletextcolor" name="example_text_color" value="<?php echo $example_text_color ?>"/><br />
<label for="exampletextsize">#example text size</label><br />
<input type="text" id="exampletextsize" name="example_text_size" value="<?php echo $example_text_size ?>"/><br /><br />
<input name="submit" type="submit" value="submit" />
</fieldset>
</form>
<br />
<div id="example">
#example Lorem Ipsum herp derp
</div>
<?php
} else {
// We set the cookie expiration time to 1 month on from the _update_ of values
$month = 2592000 + time();
// We need to serialize the $style array as we cannot store the array directly in the cookie
$style = serialize($_POST);
// We set the cookie name to "style" and put in our serialized array $style and expiration date $month
setcookie("style", $style, $month);
// Reload the page to make sure changes take effect
header("Location: index.php");
}
?>
</body>
</html>
<?php
header("Content-type: text/css");
// Check if the cookie is already set, otherwise set some default styling options
// Also, we need to unserialize the value of the cookie as we are working with arrays
$style = !empty($_COOKIE['style']) ? unserialize($_COOKIE['style']) : null;
// First check if the keys in the arrays are empty, then check if it starts with #
$body_background_color = !empty($style['body_background_color']) ? (strpos($style['body_background_color'],"#") !== 0 ? "#". $style['body_background_color'] : $style['body_background_color']) : "#fff";
$body_text_color = !empty($style['body_text_color']) ? (strpos($style['body_text_color'],"#") !== 0 ? "#". $style['body_text_color'] : $style['body_text_color']) : "#000";
$body_text_size = !empty($style['body_text_size']) ? $style['body_text_size'] : 12;
$example_background_color = !empty($style['example_background_color']) ? (strpos($style['example_background_color'],"#") !== 0 ? "#". $style['example_background_color'] : $style['example_background_color']) : "#fff";
$example_text_color = !empty($style['example_text_color']) ? (strpos($style['example_text_color'],"#") !== 0 ? "#". $style['example_text_color'] : $style['example_text_color']) : "#000";
$example_text_size = !empty($style['example_text_size']) ? $style['example_text_size'] : 10;
?>
body {
background-color: <?php echo $body_background_color ?>;
color: <?php echo $body_text_color ?>;
font-size: <?php echo $body_text_size ?>px;
}
#example {
background-color: <?php echo $example_background_color ?>;
color: <?php echo $example_text_color ?>;
font-size: <?php echo $example_text_size ?>px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment