views:

478

answers:

1

I have a site that uses www.example.com for standard pages and secure.example.com for HTTPS. I am trying to set a cookie when user logs in that will be valid on both the HTTP & HTTPS versions of the site.

I am doing this by setting path to "/" and domain to ".example.com". This works fine in Firefox and Internet Explorer, but in Chrome the cookie is only working on the version of the site where it was set (http://www.example.com or https://secure.example.com)

Is this a bug or am I doing something wrong? If it's a bug is there a workaround?

The cookie is being set by PHP in headers.

setcookie("login",base64_encode($email."::".md5($password)),2840184012,"/",".example.com");
+2  A: 

You cannot set a cookie for both HTTP and HTTPS at the same time. You need to set two separate cookies, one for HTTP and one for HTTPS:

setcookie("login", base64_encode($email."::".md5($password)), 2840184012, "/", ".example.com");
setcookie("login", base64_encode($email."::".md5($password)), 2840184012, "/", ".example.com", true);

This does only work if you set the cookies in https://secure.example.com as you can only set secure cookies via HTTPS.

Oh, and by the way: Do not store the authentication information in a cookie! Use a once valid authentication token instead.

Gumbo
Tried as you suggested but this still doesn't work in Chrome. I am using header("Location: http://www.example.com") right after setting the cookie if this makes a difference?
Tim
@Tim: And you’re setting these cookies via HTTPS?
Gumbo
Yes correct I tested on HTTPS. The cookie works on both HTTP and HTTPS sites in all my browsers except Chrome where it only works on the HTTPS site.
Tim
@Tim: So the HTTP cookie is not set when requesting via HTTPS?
Gumbo
Chrome sends the cookie to server on HTTPS pages but is not sending it to the HTTP pages. Here is the header being sent by server:Set-Cookie login=blahblahblah; expires=Thu, 01-Jan-2060 12:00:12 GMT; path=/; domain=.example.com login=blahblahblah; expires=Thu, 01-Jan-2060 12:00:12 GMT; path=/; domain=.example.com; secure
Tim
@Tim: I think Chrome assumes that you want a secure cookie if you set it via HTTPS. So it only stores one. Did you try it with different cookie names?
Gumbo
Sorry for the delayed response. Using different cookie names did the trick. Although it didn't work for overwriting the cookie later when user logs out, I had to overwrite the HTTPS one first then redirect to HTTP page to overwrite the standard one. Thanks for your help with this.
Tim