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.

Written by Tim on May 22nd, 2007 with 4 comments.
Read more articles on asp.net and php and web 2.0 ish.



Related articles

4 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com nike shox torch
#1. May 4th, 2011, at 8:04 AM.

I’m interested to know if it’s possible to copy a paragraph of this publication to use for my school project.

Get your own gravatar by visiting gravatar.com Griffey Shoes
#2. August 15th, 2011, at 1:48 AM.

Thank you for the article, I saw after the enlightened, my idea like you, just not good at expression

Get your own gravatar by visiting gravatar.com Mens Sandals
#3. September 28th, 2011, at 9:01 AM.

Necessity never makes a good bagain. ?Benjamin Franklim, American Pesident?

Get your own gravatar by visiting gravatar.com sac louis vuitton
#4. November 19th, 2011, at 6:30 AM.

At Louis Vuitton gradually establish a brand image, by the greedy imitators to its successful steal sac louis vuitton. But this further inspired George Vuitton Creativity — 1896, his Monogram canvas printed on the famous” LV” logo sacs louis vuitton, this makes the Louis Vuitton began as a symbol of the brand into the idea of people louis vuitton pas cher.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




Anti-Spam Protection by WP-SpamFree

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .

Famous Homemade Soups