I have tried to set a cookie on this page, like this:
<?php
setcookie("fasciaoffer", $name, time()+36000);
?>
I have verified that the cookie is set.
I then want to retrieve the cookie when my customer views their shopping basket and display a message:
<?php
if (isset($_COOKIE["fasciaoffer"]))
echo "<b>Special offers available</b> - buy any 3 covers & get your 4th choice free! " . $_COOKIE["fasciaoffer"] . "!<br />";
else
echo "<b>Special offers available</b> - none available on current order<br />";
?>
However, the message that comes up in my shopping basket is the one where the cookie has not be found.
What could the problem be? Is my coding correct?
PS. The file which the retrieve bit is in (the shopping basket) is not in the root folder.
I’ve always had an interesting time when working with cookies. But here are my thoughts :
1) Are you sure $name is passing a value? To check this, echo out $name after you place your cookie. I usually test my vars with echo(“name : $name
“); just to make sure that my head and PHP are on the same page. If you recieve a null value, there is your problem.
2) I usually find it benefitial to do a page refresh before reading in any cookies. Though you should put a qualifier like if (!isset($_COOKIE[‘fasciaoffer’])) { set cookie code }
I hope that helps.