Setting cookie path/domain

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Setting cookie path/domain

Post by ben.artiss »

Hi, I've just wrapped my head around paypal's API, so I decided to make a simple shopping cart just to test it out - but I'm having a little trouble because of not setting the right path/domain for the cookies.

As it stands I have '/index.php' which has a short list of items that have an 'Add to cart' link which sends the item ID to '/lib/basket_add.php?item=numericID'. I know it would be fine if I just moved basket_add.php to the root of the site, but I don't want to! :wink: So I've tried the following but it's not working out for me:

Code: Select all

setcookie("basket[0]", $cookie_input, time()+(3600*24*31), 'http://localhost/pptest/');
setcookie("basket[0]", $cookie_input, time()+(3600*24*31), '/', 'http://localhost/pptest/');
I looked at the manual but it says you can specify two paths. Surely if the first argument says where to set the cookie in the domain specified by the second argument... the second example above should work!?

Any tips (or hidden gems) would really be appreciated! Thanks, Ben

Sorry! Sorted it! :)

This site recommends

Code: Select all

setcookie("username", "George", false, "/", false);
Post Reply