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?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Samo Adx
  • 61
  • 7
  • try it with out domain or path –  Apr 16 '13 at 01: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. – David Apr 16 '13 at 01:18

2 Answers2

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.

gview
  • 14,876
  • 3
  • 46
  • 51
  • 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. – David Apr 16 '13 at 02:53
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.

David
  • 17,673
  • 10
  • 68
  • 97
  • 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? – Samo Adx Apr 17 '13 at 23:18
  • @SamoAdx Yes, I am an idiot. It's supposed to be 127.0.0.1 my.fake.local. I had them switched around :( – David Apr 18 '13 at 00:33
  • 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. – Samo Adx Apr 18 '13 at 21:55
  • @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 ). – David Apr 18 '13 at 22:33