• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP New to php, help needed!


Hello. I am re-writing a Classic ASP website in PHP, which I am new to, but learning fast. I have got a long way with the project but I have now hit a problem which I have been trying to overcome for some days, but everything I try seems to run into a brick wall. It's like this...
I have a page 'list-content.php' which extracts a list of music titles from a database and displays them in a list, together with an 'add to cart' button for each one.
On the page I have a cart icon which updates itself and displays a number which is the number of items in the cart. Clicking the icon opens the 'cart.php' page in the same window, thereby closing the 'list-content' page. A button on the 'cart.php' page re-opens 'list-content' again.
Items which are added to the cart are stored in a database table along with a unique order number, and the plan is to delete the order once it has been paid for (via PayPal) and processed. Here is the problem:
I need to store the order details on the users device, so that if the user closes the browser without completing the order he can come back to it at a later stage and the cart will still be able to pick up the details from the database. I have tried numerous ways to do this. I'm sure it can be done because I have experienced only recently a well known DIY store's website which still had the details of what I had placed in my cart when I looked some days later.
This is causing massive brain-ache and if anyone can show me how to do this I promise I will love you forever!


King Coder
Staff Team
Hey and welcome :D you are in need of cookies and no not the ones you eat :D
Have a look at the link and tell me if this helps :D


Hi, thank you for your swift response! It certainly looks as if Cookies might do the trick. I've looked at the link, however when I try the examples they provide I get this:
"Cookie named 'user' is not set!
Note: You might have to reload the page to see the value of the cookie."

Reloading the page has no effect. Any idea why this might be?


Hey and welcome :D you are in need of cookies and no not the ones you eat :D
Have a look at the link and tell me if this helps :D
Further to my earlier reply, just tried it on my smartphone and got the same response, maybe it's a problem at their end, anyway I'll have a go at incorporating cookies into my code and let you know how I get on.


And further still...

I have tried to set a cookie in my code but I am still getting the same response. Here is my code:
$cookie_name = "item_name";
$cookie_value = "TRM FOR TESTING"; //$name;
//setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
setcookie("item_name", "TRM FOR TESTING", time() + (86400 * 30), "/"); // 86400 = 1 day

if(!isset($_COOKIE["item_name"])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$item_name];
You can see that I have tried using vars for the cookie name and value as shown in the link you sent, and also tried hard coding the name and value but it seems the cookie is still not being set. Any idea what I'm doing wrong?


Aha! I failed to notice the dictum that the setcookie() function must appear BEFORE the <html> tag.
Silly me!, although I am somewhat encouraged by the fact that this rule is not adhered to in W3 schools own example, which is no doubt, the reason why it doesn't work...