1

I'm trying to set a session and a cookie for when user logs in.

When the user visits the login page, a session is set and started, with session_start() which is working quite alright, but when the user now fills in the login form (with username and password) and the proper check is done for correct login details, I set the cookie:

$one_week = 60*60*24*7; setcookie("cookiejarcookie", "cookiejar_value", time()+$one_week, '/', 'localhost'); 

It's not working, the cookie is not being set. I've tried calling it from the top of the script, but it's not working.

How do I set the cookie after setting the session?

2
  • try it with out domain or path Commented Apr 16, 2013 at 1:16
  • 1
    You cannot set cookies to localhost, but if you add a my.fake.local in your hosts file ( /etc/hosts or c:\Windows\System32\drivers\etc\hosts ) that should work. Commented Apr 16, 2013 at 1:18

2 Answers 2

1

Trying to set a cookie on localhost does not work in most browsers. You need to set the domain value to null, empty string or false. Most recommendations I've seen are to set the domain value to false. With that said, I've never understood writing code like that, as it is not something you're going to deploy to a production environment.

See the recommendation by @David. I personally use virtualization to run a server environment and map fake dns using the hosts file.

One tip I can offer is that you have to open your editor (I use notepad++ or wordpad) as administrator on most recent versions of windows that have UAE in order to edit the relevant hosts file.

Sign up to request clarification or add additional context in comments.

1 Comment

Virtualization is the best way to go, been using vagrant a lot more recently and its ridiculous how fast it is to get a cleanslate dev environment going.
1

From my comment

You cannot set cookies to localhost, but if you add a my.fake.local in your hosts file ( /etc/hosts or c:\Windows\System32\drivers\etc\hosts ) that should work.

add

127.0.0.1 my.fake.local 

in the appropriate hosts file.

4 Comments

i tried dragon's method, setting the cookie without the domain, and it worked,setcookie("cookiejarcookie", "cookiejar_value", time()+$one_week,); but when i tried doing it using David's description, it's not working, set the custom host name in my host file in the system32 directory, it's not working, is there something i'm getting wrong here?
@SamoAdx Yes, I am an idiot. It's supposed to be 127.0.0.1 my.fake.local. I had them switched around :(
Thanks alot david, it worked, now i get the logic, what it does is, it maps 127.0.0.1 to my.fake.local, just as 127.0.0.1 maps to localhost, so when ppl types in localhost in the browser, it resolves to 127.0.0.1. Thanks.
@SamoAdx For development purposes, modifying your hosts file can be fairly useful, especially if you follow gview's suggestion about using virtualization ( virtualbox + vagrant can allow you to build arbitrary virtual machines and then add them to your host file so you don't have to remember the IP ).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.