Jump to content

PHP cookie setting will not work, no sleep.


dcp3450

Recommended Posts

I have been trying to get my code to work for days. All I'm trying to do is allow the user to choose a theme for the site. Here is the flow:

 

User comes to page and views a default theme in css -> User chooses new theme from drop down and presses go button -> new theme is stored in a cookie -> page reloads, sees cookie, and changes the theme

 

The cookie is being set and the value changes between cardboard, paper, and watercolor. However, it looks like the cookie isnt being read.

 

I've tried many many ways to call the cookie based off my knowledge, friends that code php, and even tutorial that show how to do it.

 

here is the code minus the html-

 

index.html call for cookie:

<link rel="stylesheet" type="text/css" 	media="screen" title="User Defined Style" href="<?php echo . $_COOKIE["style"]; ?>.css" />

 

index.html user selects theme:

<form action="csschoose.php" method="post">
          <select name="choice">
            <option selected>Choose a theme</option>
            <option value="cardboard">Cardboard</option>
            <option value="paper">Paper</option>
            <option value="watercolor">Watercolor</option>
          </select>
	  <input type="submit" value="GO">
        </form>

 

csschoose.php sets the cookie name:

$year =31536000 + time();
$csschoose = $_POST['choice'];
setcookie ('style', $csschoose, $year);
/*header("Location: $HTTP_REFERER"); */
if($_SERVER['HTTP_REFERER'] != "") 
{
header("Location: " . $_SERVER['HTTP_REFERER']);
} 
else 
{
header("Location: (site name is here)");
}

 

you can't get value in the $_COOKIE variable if your code doesn't output in browser first.

you use

setcookie ('style', $csschoose, $year);

 

but then you do not output that script, u use just a header redirect of php. Then u want to access the variable using $_COOKIE. thats why it didn't work. U can use session for this so that u don't need to output in the browser first.

 

Archived

This topic is now archived and is closed to further replies.

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