Jump to content

Recommended Posts

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 == 8)

$thecolor = "e2ff20"; // LIME

if ($num == 9)

$thecolor = "8868ff"; // LAVENDER

// send a cookie that never expires

setcookie("thecolor",$thecolor, time()+604800);

}

?>

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

 

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 == 8)

$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

 

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);
}
?>

 

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

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.

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);

}

 

?>

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);

 

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.