trying to make a color switcher (using sessions)
Posted: Tue Aug 19, 2008 7:35 pm
Hello. First post here.
I'm new to PHP and am trying to learn how to write a little code that switches background colors on my website. What I DONT want is the colors to switch every time someone refreshes or clicks a page. That is why I am trying to use the $_SESSIONS variable, so the website remains one color per session.
That is exactly what is happening....
This is what I have thus far...
And in my HTML, I have this.....
Every time I refresh the colors change. I understand WHY this is happening (the $_SESSIONS is getting set back to NULL upon every refresh), but I cant figure out a way to fix it!
Any input would be awesome!
THX...
George
I'm new to PHP and am trying to learn how to write a little code that switches background colors on my website. What I DONT want is the colors to switch every time someone refreshes or clicks a page. That is why I am trying to use the $_SESSIONS variable, so the website remains one color per session.
That is exactly what is happening....
This is what I have thus far...
Code: Select all
<?php
function randomColor(){
$_SESSION['cssSet']= null;
$colorArray = array(
'dddddd',
'00ff00',
'ffffdd',
'0000ff'
);
if(isset($_SESSION['cssSet'])){
return;
}elseif(empty($_SESSION['cssSet'])){
$totalColors = count($colorArray);
$i = mt_rand(0, $totalColors-1);
$chosenColor = $colorArray[$i];
$_SESSION['cssSet']=1;
$_SESSION['color']=$chosenColor;
return $_SESSION['color'];
}
}
?>
And in my HTML, I have this.....
Code: Select all
<style type="text/css">
body{
background-color: <?php echo '#' . randomColor(); ?>;
}
</style>
Any input would be awesome!
THX...
George