February 28, 2012

set cookie then redirect to another domain

Question by mulllhausen

i’m not sure if i’m doing something wrong in my code or if my desired action is just not allowed in the standards…i’m trying to set a cookie on my localhost then redirect the user off to another domain. (for some background on why i would want to do this: as part of the oauth 1.0 signin process i want to set the oauth state then redirect the user off to the service provider to signin; but this is not really relevant to my question – it just may be useful in case someone wants to suggest a better way to do what i am trying to do…)

here is a simpified version of the relevant piece of my php code:

setcookie('oauth_state', 'one', time() + 60*60, '/', 'localhost', false, false);
header("Location: http://google.com/");
exit;

i’m testing on firefox 10.0.2 on ubuntu and no cookie at localhost is being set. what am i doing wrong?

Answer by jjs9534

Cookies are domain specific. So if you set the cookie in one domain and then redirect to a new domain, then the cookie you previously set will not be visible.

Answer by Starx

The time is set to 3600 which is prehistoric.

Change this to time()+3600 it will work.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!