Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a cookie on localhost with MAMP + MacOSx + PHP?

Tags:

php

cookies

mamp

I'm developing on my Mac notebook, I use MAMP. I'm trying to set a cookie with PHP, and I can't. I've left off the domain, I've tried using "\" for the domain. No luck.

setcookie("username", "George", false, "/", false);
setcookie("name","Joe");

I must be missing something obvious. I need a quick and simple solution to this. Is there one?

I'm not doing anything fancy, simply loading (via MAMP) the page, http://localhost:8888/MAMP/lynn/setcookie.php

That script has the setcookie code at the top, prior to even writing the HTML tags. (although I tried it in the BODY as well). I load the page in various browsers, then open the cookie listing. I know the browsers accept cookies, because I see current ones in the list. Just not my new one.

like image 349
lynn Avatar asked Jan 24 '23 18:01

lynn


1 Answers

From the docs:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

Is that it?

edit:

Can you see the cookie being sent by the server, e.g. by using the Firefox extension Tamper Data, or telnet? Can you see it being sent back by the browser on the next request? What's the return value of setcookie()? Is it not working in all browsers, or just in some?

like image 197
Simon Avatar answered Jan 29 '23 19:01

Simon