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