Style switcher with PHP and CSS
i have this simple style swicher
style.css
body.defualt { background-color: RGB(230,197,173);
background-image:url("img/defualt.gif") ;
background-repeat: no-repeat ;
font-family : Lucida Handwriting;
color : white;
}
body.spring {
background-image:url("img/spring.png");
background-repeat: repeat ;
font-family : Arial Rounded MT Bold;
}
body.winter{
background-image:url("img/winter.png");
background-repeat: repeat ;
font-family : Comic Sans MS;
color : RGB(53,31,99);
}
a {text-decoration : none;
color :RGB(175,48,175);
}
a:hover {text-decoration :underline ;
text-transform: uppercase;
}
now when i change defualt style it's changed but when make refresh it's return to defualt i want know how to save my style
<?php
if ($GLOBALS["language"]==arabic)
{include ("ar.php");}
else {include ("en.php");}
?>
<html >
<head>
<title>
MultiMedia
</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="function.js" > </script>
</head>
<body class="defualt">
<br/><br/开发者_如何学编程>
<!-- header -->
<!-- MultiMedia -->
<h1 ><center><?php echo $header ; ?></center></h1>
<!-- theme selector -->
<table style="position:fixed;left:25px;top:210px;" >
<tr> <th> <?php echo $choose?><th> <tr/>
<tr>
<td align=center>
<a onclick ="change('defualt');" title="Defualt" ><img src="img/defualtTHM.gif" style="border-color:white;" border=2 /> </a>
</td>
</tr>
<tr>
<td align=center>
<a onclick ="change('spring');" title="Spring" ><img src="img/springTHM.png" style="border-color:white;" border=2 /> </a>
</td>
</tr>
<tr>
<td align=center>
<a onclick ="change('winter');" title="Winter" ><img src="img/winterTHM.png" style="border-color:white;" border=2 /> </a>
</td>
</tr>
Keep it in the session.
If you're using PHP, send it to your controller and then store the value in your session. Next time when you load the PHP file, check your session, if the key is set, then load that CSS file otherwise load the default
switchstyle.php
$style = isset($_GET['style']) ? $_GET['style'] : false;
if ($style) {
$_SESSION['custom_style'] = $style;
}
In index.php (assuming that's your view
if (isset($_SESSION['custom_style'])) {
//load the style
} else {
//load default style
}
NOTE This is an example/suggestion only. You should validate your $_GET and $_SESSION checks so you stay protected.
You probably want to store (and set) language as a $_SESSION
variable, not a $_GLOBALS
variable.
For example, try this:
session_start();
$_SESSION['language'] = "arabic";
You can then change $_SESSION, and it'll be tracked (for that user only) across pages, as long as you call session_start();
before you try and access or change any $_SESSION
variable.
it would be nice if we could see the "change" function but I guess you need this
<script type="text/javascript">
function change(theme) {
var date = new Date ();
date.setDate(date.getDate() + 3); // this theme will stay 3 days
document.cookie = 'theme=' + theme + '; expires=' + date.toUTCString()
}
</script>
and in the html
<body class="<?=isset($_COOKIE['theme'])?$_COOKIE['theme']:'default'?>">
精彩评论