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 Reading and Deleting Cookies

Martin Park
Martin Park
12,792 Points

Cookie will not delete in Workspaces?

I've watched the video several times. I've rewritten the code several times. I've even tried deleting the cookie manually but it still remains. What am I doing wrong? Any help greatly appreciated

============================

index.php

if (isset($_COOKIE)){

//var_dump($_COOKIE);

foreach ($_COOKIE as $key => $value) { if ($key != 'PHPSESSID') { echo '<div class = "form-group">';

echo '<a class="btn btn-info" href="inc/cookie.php?read=' . urlencode($key) . '">';
echo substr($key, 0, -10);
echo ' ';
echo date('d M Y H:i:s', (int) substr($key, -10));
echo '</a>';

echo '<a class="btn btn-danger" href="inc/cookie.php?delete=' . urlencode($key) . '">';
echo 'X</a>';

echo '</div>';

}

}

}

============================

cookie.php

session_start();

if (isset($_GET['save'])){ $name = urlencode($_SESSION['word'][2]) . time(); setcookie($name, implode(':', $_SESSION['word']), strtotime('+30days'), '/');

} elseif (isset($_GET['read'])) {

$_SESSION['word'] = array_combine(range(1,5), explode(':', $_COOKIE[$_GET['read']]));
header('location: /story.php');
exit;

} elseif (isset($_GET['delete'])) {

setcookie($_GET['delete'], "", time() - 3600, '/');

}

header('location: /index.php'); exit;

===========================

Martin Park
Martin Park
12,792 Points

Alena Holligan Can you help with this please? :)

Martin Park
Martin Park
12,792 Points

I'm going to have to move on without having solved this.

I should add if I use code:

setcookie("PHPSESSID", "", time() - 3600, '/');

I can remove the PHPSESSID element from the array but the same code will not work for the various stories stored in the cookie. I'm either doing something stupid or there is a problem with Workspaces?