Subdomain Cookies and Localhost

So I have a site – we’ll call www.site1.com that sets a client side cookie named CookieName1 and I need a subdomain test.site1.com to be able to read that value. Using php’s standard setcookie() method I set the cookie on www by using:

setcookie( “CookieName1”, $CookieName1Value, $CookieExpDate, “/”, “.site1.com” );

This works, no problem.  Now test.site1.com uses .NET to read in the value and do something.  Pretty standard stuff here:

  If Request.Cookies(“CookieName1”) IsNot Nothing Then

   ”DO SOMETHING

  End If

 

Build > Debug, set a breakpoint on the if block, skips right over it.  Huh?  I make sure IE has the cookie, try again.  Nope, Request.Cookies(“CookieName1”) is Nothing.  Can’t be an IE thing can it?  Run same scenerio in FireFox – same result.  Then it dawns on me, I’m not running this under test.site1.com, I’m running this under localhost.  Everything was working as expected, cookies can only be read by *.site1.com.  So how am I going to test this thing on my local machine? I do have a sandbox, test1sandbox.site1.com, but what if I didn’t?  A simple solution is to change the host file found at %SystemRoot%\system32\drivers\etc\ with:

127.0.0.1 localhost
127.0.0.1 test1.site1.com

Run another Debug, change the http://localhost to http://test1.site1.com and now I’m able to read cookies from *.site1.com. Nifty.

Tim

A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

12 thoughts on “Subdomain Cookies and Localhost

  1. Just got to the work of using cookies accross subdomains. I came back to get your example, and, it works like a charm!

    Thanks for posting this information.

  2. YES BUT … Place a breakpoint somewhere like Global.asax or in a page and check the value in Request.URL

    You will find it reads ‘http://localhost:…’ not ‘http://test.localhost:…’ so your results are being returned (and your cookie read) from the domain not the subdomain. This debugging trick works by ‘faking’ the subdomain to be the localhost domain. Any code you have that depends on Request.URL being correct will no longer work.

    Is there a better fix for this? I haven’t found one yet!

  3. YES BUT … I found the answer. You need to check the HOST header in the request which has the full domain name (e.g. subdomain.localhost:XXXX) instead of using Request.URL.Authority. To do this I created an extension method in the Uri object which does this check and returns a ‘hacked’ version of the Uri with the correct domain name inserted using Regex. Sorted.

  4. You really make it seem so easy with your presentation but I find this
    matter to be actually something that I think I would
    never understand. It seems too complicated and very broad for me.
    I’m looking forward for your next post, I will try to
    get the hang of it!

  5. Masterly done this job you guys are doing great job to promote this field and it is very compulsory to have promotion due to the advancement and the requirement as well i have been studying your article it was making me stuck to one point that i was not able to move on and understand your words therefore i decided to get my self entertained therefore i went to this site http://www.india4moviez.com after little searching it is great as your blog is because they are entertaining people completely moreover they are writing for their visitors which is the additional aspect of the site.

  6. Another key to getting an auto you can bear the cost of every month is to be cautious of dealerships that need to place you into a fresh out of the box new auto. Where it is valid, there are some sales representative who will offer you an advance on another, never been driven vehicle, this may not be the best decision.

  7. So go online and pick the bank that best suits your needs.Thought with respect to profiting by money related help from auto credit organizations before selecting the best association that may be you could get auto account is an awesome arrangement. It is conceivable to find and additionally assess their one of a kind quotes by visit in their sites.

Leave a Reply

Your email address will not be published. Required fields are marked *