trying to make a color switcher (using sessions)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
general_salt
Forum Newbie
Posts: 1
Joined: Tue Aug 19, 2008 7:29 pm

trying to make a color switcher (using sessions)

Post by general_salt »

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...

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>
 
 
 
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
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: trying to make a color switcher (using sessions)

Post by nowaydown1 »

Hi George,

Welcome to the forum! From what I can see, I would guess that this line is what's causing your trouble:

Code: Select all

 
$_SESSION['cssSet']= null;
 
I would guess you probably added it to initialize the variable. The problem comes in during your isset() check. You're expecting isset to return true once your $_SESSION['cssSet'] variable has a value. If you look at the manual page for isset (http://us.php.net/isset) you'll find the following note:
isset() will return FALSE if testing a variable that has been set to NULL.
So in your script then, your isset($_SESSION['cssSet']) check returns false because you set it to null at the start of your function. Therefore your return never fires, and you land in your elseif. The empty function (http://us.php.net/empty) on the other hand, returns true on a null value, and your code to generate the page color fires again.

Hope that helps.
Post Reply