ibr4n Posted May 27, 2011 Share Posted May 27, 2011 I've created a code to allow the user to change the background color and scheme of my site. However, I'm having trouble getting a random color to show up if the user has not yet set a color choice. Here's my code. Could someone put it together to make it work? <?php if ($_POST['change']){ $thevar = $_POST['change']; $_SESSION['bcg'] = $thevar; } if(isset($_SESSION['thecolor'])) { $thecolor = $_SESSION['thecolor']; } else { $num = rand(0,9); if ($num == 0) $thecolor = "0DEBDF"; // CYAN if ($num == 1) $thecolor = "AE00FF"; // PURPLE if ($num == 2) $thecolor = "FFDC01"; // YELLOW if ($num == 3) $thecolor = "FF6D2D"; // PEACH if ($num == 4) $thecolor = "20ff7f"; // LIGHTER-GREEN if ($num == 5) $thecolor = "00E612"; // GREEN if ($num == 6) $thecolor = "E6005D"; // PINK if ($num == 7) $thecolor = "E600CF"; // PINK-PURPLE if ($num == $thecolor = "e2ff20"; // LIME if ($num == 9) $thecolor = "8868ff"; // LAVENDER // send a cookie that never expires setcookie("thecolor",$thecolor, time()+604800); } ?> Quote Link to comment Share on other sites More sharing options...
mellis95 Posted May 27, 2011 Share Posted May 27, 2011 Depending on how $_SESSION['thecolor'] gets set, you might have better luck checking if it is empty instead of isset. Isset will always evaluate true if $_SESSION['thecolor'] exists and is not NULL. For instance, $_SESSION['thecolor']='' will evaluate true using isset. It will evaluate correctly in your code if you use !empty. For example: if(!empty($_SESSION['thecolor'])) { $thecolor = $_SESSION['thecolor']; } else { $num = rand(0,9); ......... Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 This is my code now. <?php if ($_POST['change']){ $thevar = $_POST['change']; $_SESSION['bcg'] = $thevar; } if(!empty($_SESSION['thecolor'])) { $thecolor = $_SESSION['thecolor']; } else { $num = rand(0,9); if ($num == 0) $thecolor = "0DEBDF"; // CYAN if ($num == 1) $thecolor = "AE00FF"; // PURPLE if ($num == 2) $thecolor = "FFDC01"; // YELLOW if ($num == 3) $thecolor = "FF6D2D"; // PEACH if ($num == 4) $thecolor = "20ff7f"; // LIGHTER-GREEN if ($num == 5) $thecolor = "00E612"; // GREEN if ($num == 6) $thecolor = "E6005D"; // PINK if ($num == 7) $thecolor = "E600CF"; // PINK-PURPLE if ($num == $thecolor = "e2ff20"; // LIME if ($num == 9) $thecolor = "8868ff"; // LAVENDER // send a cookie that never expires setcookie("thecolor",$thecolor, time()+604800); } ?> However, I'm now receiving this error. Warning: Cannot modify header information - headers already sent by (output started at /home/ibran/public_html/wp-content/themes/iBran 5/header.php:9) in /home/ibran/public_html/color-change/stat.php on line 36 Quote Link to comment Share on other sites More sharing options...
mellis95 Posted May 27, 2011 Share Posted May 27, 2011 Try adding ob_start(); to the first line of the code. Like this. <?php ob_start(); if ($_POST['change']){ $thevar = $_POST['change']; $_SESSION['bcg'] = $thevar; } if(!empty($_SESSION['thecolor'])) { $thecolor = $_SESSION['thecolor']; } else { $num = rand(0,9); if ($num == 0) $thecolor = "0DEBDF"; // CYAN if ($num == 1) $thecolor = "AE00FF"; // PURPLE if ($num == 2) $thecolor = "FFDC01"; // YELLOW if ($num == 3) $thecolor = "FF6D2D"; // PEACH if ($num == 4) $thecolor = "20ff7f"; // LIGHTER-GREEN if ($num == 5) $thecolor = "00E612"; // GREEN if ($num == 6) $thecolor = "E6005D"; // PINK if ($num == 7) $thecolor = "E600CF"; // PINK-PURPLE if ($num == $thecolor = "e2ff20"; // LIME if ($num == 9) $thecolor = "8868ff"; // LAVENDER // send a cookie that never expires setcookie("thecolor",$thecolor, time()+604800); } ?> Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 No dice. Received a syntax error. Quote Link to comment Share on other sites More sharing options...
mellis95 Posted May 27, 2011 Share Posted May 27, 2011 What does the syntax error say? I haven't dealt with setcookie, but from what I understand it needs to be the first thing done in a script, but can be set after other script processing by buffering the output with ob_start(). Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 I'm getting a " Parse error: syntax error, unexpected ';' in /home/ibran/public_html/color-change/stat.php on line 32" error. Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 The value and ) for if ($num == is missing. Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 What do you mean? Can you show me what I need to add? Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 I wonder why it put that smiley face on my post. Must be something from post above that got copied. In any case the line, if ($num == is missing the ... and seems to have that smiley face in it's place. Interesting. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted May 27, 2011 Share Posted May 27, 2011 I wonder why it put that smiley face on my post. Must be something from post above that got copied. In any case the line, if ($num == is missing the ... and seems to have that smiley face in it's place. Interesting. That's the board software converting an 8 and ) into a bb smiley. Doesn't happen when you use code tags around the code. Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 I actually do have that in my code. I think the forum messed it up. This is my code. <?php if ($_POST['change']){ $thevar = $_POST['change']; $_SESSION['bcg'] = $thevar; } if(!empty($_SESSION['bcg'])) { $thecolor = $_SESSION['bcg']; } else { $num = rand(0,9); if ($num == 0) $thecolor = "0DEBDF"; // CYAN if ($num == 1) $thecolor = "AE00FF"; // PURPLE if ($num == 2) $thecolor = "FFDC01"; // YELLOW if ($num == 3) $thecolor = "FF6D2D"; // PEACH if ($num == 4) $thecolor = "20ff7f"; // LIGHTER-GREEN if ($num == 5) $thecolor = "00E612"; // GREEN if ($num == 6) $thecolor = "E6005D"; // PINK if ($num == 7) $thecolor = "E600CF"; // PINK-PURPLE if ($num == 8 ) $thecolor = "e2ff20"; // LIME if ($num == 9) $thecolor = "8868ff"; // LAVENDER setcookie("thecolor", $thecolor, time()+604800); } ?> Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 Well you do have a space after the EIGHT. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted May 27, 2011 Share Posted May 27, 2011 Do you have session_start(); anywhere in your code? I'm not seeing in the code you posted. You need it in each document that sets or calls a session variable. Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 I've added session start, and I added the space, so the wouldn't come up in the forum. Quote Link to comment Share on other sites More sharing options...
mellis95 Posted May 27, 2011 Share Posted May 27, 2011 I have tested the code below and it works perfectly on my setup. I should have noticed last night that you were only setting the cookie in the else statement. I'm not sure if that was the problem or not, but the only things I changed was where the cookie was set and I added curly braces to all of the if statements, just because I think it is good practice and makes the code more readable. With the code below I can continue refreshing the page and get a different random color on each refresh. I left the die() in there so you can verify the color change in the browser. Just take that out and you should be ready to go. <?php session_start(); ob_start(); if ($_POST['change']) { $thevar = $_POST['change']; $_SESSION['bcg'] = $thevar; } if(!empty($_SESSION['bcg'])) { $thecolor = $_SESSION['bcg']; } else { $num = rand(0,9); if ($num == 0) { $thecolor = "0DEBDF"; // CYAN } if ($num == 1) { $thecolor = "AE00FF"; // PURPLE } if ($num == 2) { $thecolor = "FFDC01"; // YELLOW } if ($num == 3) { $thecolor = "FF6D2D"; // PEACH } if ($num == 4) { $thecolor = "20ff7f"; // LIGHTER-GREEN } if ($num == 5) { $thecolor = "00E612"; // GREEN } if ($num == 6) { $thecolor = "E6005D"; // PINK } if ($num == 7) { $thecolor = "E600CF"; // PINK-PURPLE } if ($num == { $thecolor = "e2ff20"; // LIME } if ($num == 9) { $thecolor = "8868ff"; // LAVENDER } } setcookie("thecolor", $thecolor, time()+604800); die($thecolor); Quote Link to comment Share on other sites More sharing options...
ibr4n Posted May 27, 2011 Author Share Posted May 27, 2011 It doesn't seem to be showing any page colors. Quote Link to comment Share on other sites More sharing options...
mellis95 Posted May 28, 2011 Share Posted May 28, 2011 I don't know what to tell you. It works fine on my setup. If you copy the code exactly as I have it in the post it should have a blank screen with a random color code (ex. FF6D2D) displayed on the screen. Are you saying it is not even doing that? Or is it not changing your background color the way you want. If the latter is the case, the problem is likely in the way you apply the random code the the background style. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.