Menu
I saw this in another thread once, I just can’t find it now. I need to be able to let my users pick the background color that they like for my site, and have it be saved in a cookie. Thank you,
Jeffrey
[code=php]
$bgcolor = $_POST['bgcolor'];
setcookie( "bgcolor", $bgcolor, 2147483647 );
[/code]
[code=php]
<body bgColor="<?php echo ($_COOKIE['bgcolor']) ? $_COOKIE['bgcolor'] : '#FFFFFF';?>">
[/code]
[code=php]
if ($color == "FFFFFF")
echo ('<form action="index.php" method="post"><input type=hidden name=bgcolor value=FFFFCC><input type="submit" value="Origianl"></form>');
else echo('<form action="index.php" method="post"><input type=hidden name=bgcolor value=FFFFFF><input type="submit" value="White"></form>');[/code]
[code=php]$color = $_COOKIE['bgcolor'];
$bgcolor = $_POST['bgcolor'];
if ($color == "")
setcookie( "bgcolor", "#FFFFCC", 2147483647 );
else
setcookie( "bgcolor", $bgcolor, 2147483647 );[/code]
[code=php]
if (!$_COOKIE['bgcolor'])
setcookie( "bgcolor", "#FFFFCC", 2147483647 );
else
setcookie( "bgcolor", $_POST['bgcolor'], 2147483647 );
$color = $_COOKIE['bgcolor'];
[/code]
[code=php]<?php echo ('<form action="index.php" method="post"><input type="hidden" name="bgcolors" value="s"><input type=hidden name=bgcolor value=FFFFCC><input type="submit" value="Origianl"></form>
<form action="index.php" method="post"><input type="hidden" name="bgcolors" value="s"><input type="hidden" name="bgcolor" value="CCCCFF"><input type="submit" value="New"></form>'); ?>[/code]
[code=php]<?php
$bgcolor = $_POST['bgcolor'];
setcookie( "bgcolor", $bgcolor, 2147483647 );?>
<body bgColor="<?php
if ($_POST['bgcolors'] == "s") echo $_POST['bgcolor']; else echo$_COOKIE['bgcolor'] ? $_COOKIE['bgcolor'] : '#CCCCFF';?>">[/code]
[code=html]
<html><head><title>Day Trader</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body><h2>Live in Style</h2></body></html> [/code]
[code=html]
style0.css body { font-family: serif }
h2 { color: steelblue }
style1.css body { font-family: sans-serif }
h2 { color: seagreen }
style2.css body { font-family: cursive }
h2 { color: goldenrod } [/code]
[code=html]<script>
var StyleFile = "style" + new Date().getDate() % 3 + ".css";
document.writeln('<link rel="stylesheet" type="text/css" href="' + StyleFile + '">');
</script> [/code]
[code=html]<html><head><title>Style Switcher</title>
<script>
var StyleFile = "style" + document.cookie.charAt(6) + ".css";
document.writeln('<link rel="stylesheet" type="text/css" href="' + StyleFile + '">');
</script>
</head><body><h2>Live in Style</h2><br>
<a href="javascript: document.cookie='style='; window.location.reload();">Style 1</a> |
<a href="javascript: document.cookie='style=2'; window.location.reload();">Style 2</a> |
<a href="javascript: document.cookie='style=3'; window.location.reload();">Style 3</a>
</body></html>[/code]
but that's more of a color switcher rather than a CSS switcher. A real CSS switcher should switch an external CSS.[/QUOTE]
[code=php]<?php
if ($_GET["st"] == "blue"){ // if st = blue
$style = blue; // then the style will be blue
}elseif ($_GET["st"] == "green"){ // if st = green
$style = green; // then the style will be green
}elseif (isset($_COOKIE['COOKIENAME'])) { // if a cookie is set
$style = $_COOKIE['COOKIENAME']; // then the style will be the cookie value
}else{ // if there is no cookie set
$style = green; // set the default style
}
setcookie("COOKIENAME", $style, time()+60*60*24*30, "dir","domain"); // set a cookie with the new style
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'."n";
echo '<head>'."n";
echo '<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8">'."n";
echo '<title>PHP CSS Switcher</title>'."n";
echo '<link rel="stylesheet" media="all" type="text/css" href="'.$style.'.css">'."n";
echo '</head>'."n";
?>[/code]
[code=php]
<?php
echo '<li><a href="index.php?st=green" title="Green Style">Green</a> | </li>'."n";
echo '<li><a href="index.php?st=blue" title="Blue Style">Blue</a></li>'."n";
?>
[/code]
[code=php]echo '<a href="'.$page.'?st=blue" title="Blue Style">Blue</a>';[/code]
[code=php]
if (isset($_GET["st"])){
$style = $_GET["st"];
}elseif(isset($_COOKIE['COOKIENAME'])) {
$style = $_COOKIE['COOKIENAME'];
}else{
$style = green;
}
[/code]
[code=php]
<?php
$page = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($_GET["st"])){
$style = $_GET["st"];
}elseif(isset($_COOKIE['COOKIENAME'])) {
$style = $_COOKIE['COOKIENAME'];
}else{
$style = green; // default style colour
}
setcookie("COOKIENAME", $style, time()+60*60*24*30, "/","domain"); // set a cookie with the new style
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'."n";
echo '<head>'."n";
echo '<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8">'."n";
echo '<title>TITLE</title>'."n";
echo '<link rel="stylesheet" media="all" type="text/css" href="style.css">'."n";
echo '<link rel="stylesheet" media="all" type="text/css" href="'.$style.'.css">'."n";
echo '</head>'."n";
echo '<body>'."n";
....
echo '<p>Change this sites stylesheet:<br />'."n";
echo '<ul class="horizontal">'."n";
echo '<li><a href="'.$page.'?st=green" title="Green Style">Green</a> | </li>'."n";
echo '<li><a href="'.$page.'?st=blue" title="Blue Style">Blue</a> | </li>'."n";
echo '<li><a href='''.$page.'?st=purple" title="Purple Style">Purple</a> | </li>'."n";
echo '<li><a href="'.$page.'?st=red" title="Red Style">Red</a></li>'."n";
echo '</ul>'."n";
echo '</body></html>'."n";
?>[/code]
0.1.9 — BETA 5.15