Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share cookies to subdomain on localhost

Tags:

php

cookies

I create a bunch of cookies for authentication purposes and a part of the website allows the users to navigate to the UK part of the site. This is enabled by having a uk prefix in the domain. (e.g uk.domain.com).

I basically have a small script that checks to see if the subdomain is called UK then I deliver the UK content.

How can I make it so all the cookies on domain.com transfer to uk.domain.com?

I've tried ...

... setting the cookie to the just root domain

 setcookie("auth", "blahblah", time() + 123, "/", "localhost")

... adding a dot to the beginning of the domain

 setcookie("auth", "blahblah", time() + 123, "/", ".localhost")

... creating the cookie on both domains

 setcookie("auth", "blahblah", time() + 123, "/", "localhost")
 setcookie("auth", "blahblah", time() + 123, "/", "uk.localhost")

... creating cookies without any domain or path.

 setcookie("auth", "blahblah", time() + 123, "/")
 setcookie("auth", "blahblah", time() + 123)

I just can't seem to get it to work.

like image 414
slothinspace Avatar asked Jul 30 '16 00:07

slothinspace


1 Answers

Found out that you cannot set localhost as a cookie on chrome. It needs to be a registry controlled domain and not an IP or intranet hostname.

I found a workaround by basically turning localhost into a domain.

I added this to the host file (c:\windows\system32\drivers\etc\hosts) -

127.0.0.1    localhost.com    
127.0.0.1    uk.localhost.com    

then created the cookies with the localhost domain

setcookie("auth", "blahblah", time() + 123, "/", "localhost.com")

This allowed me to access the auth cookie from http://uk.localhost.com (or pretty much any subdomain).

like image 195
slothinspace Avatar answered Sep 16 '22 15:09

slothinspace