Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Browser Persistent Data with PHP Data Persistence on the Web Cookies

i am in need

Challenge Task 1 of 1

Set a cookie named "treehouse" whose value is set to "cookie challenge". The cookie should expire in 1 hour and be available to all paths of the site.

cookies.php
<?php
//add cookie below this line
$cookie_name = "treehouse";
$cookie_value = "cookie challenge";
setcookie($treehouse, $cookiechallenge, time() +3600, "/"); 

if(!isset($_COOKIE[$treehouse])) {
    echo "Cookie named '" . $treehouse . "' is not set!";
} else {
    echo "Cookie '" . $treehouse . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$treehouse];
}
?>

2 Answers

Luc de Brouwer
seal-mask
.a{fill-rule:evenodd;}techdegree
Luc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 Points

You made this mistake: you've put in literal values in the function as parameters instead of the variables you have declared in the file.

Just use $cookie_name & $cookie_value instead of those literal values you declared.

$cookie_name = "treehouse";
$cookie_value = "cookie challenge";
setcookie($cookie_name, $cookie_value, time() +3600, "/"); 

if(!isset($_COOKIE[$treehouse])) {
    echo "Cookie named '" . $treehouse . "' is not set!";
} else {
    echo "Cookie '" . $treehouse . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$treehouse];
}
?>

Brouwer u the best

Dario Preglej
Dario Preglej
9,771 Points

setcookie("treehouse", "cookie challenge", strtotime('+1 hour'), '/');