homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Setting Cookie on redirected domain

 11:15 pm on May 20, 2010 (gmt 0)

I'm trying to track visitors who come to my site through a vanity domain that I set up years ago. What I'm trying to do is set a cookie so that I can track them. here is the code I have:

if (!isset($_COOKIE["Redirect"])) {
setcookie("Redirect", "Yes", time()+60*60*24*365, "/");
header("HTTP/1.1 301 Moved Permanently");
header("Location: [domain.com...]

This script gets called from an Apache rewrite rule. They would enter on www.vanitydomain.com and Apache does an internal rewrite to my script.

The problem is that the cookie gets created in the www.vanitydomain.com domain and not www.domain.com. I've tried moving the create cookie statements after the redirect and it still doesn't work right. Now I realize that I could add a query parameter to the end of the redirected url and pick up on it that way, but I don't want to redirect the domain to a page with a query parameter as that may be seen as a duplicate page.

Any ideas?





 6:21 am on May 21, 2010 (gmt 0)

Even if you place the setcookie call after the header, its still being executed on vanitydomain.com and therefore cookie will still be created there. Just an idea, On your domain.com check Referer, if the referer is your vanity domain.com that means user came from there and you can set cookie on domain.com. without any need of having a parameter in url


 3:06 pm on May 21, 2010 (gmt 0)

Well, yes of course I thought of that, but referer is pretty unreliable. I guess it would be better than nothing if that's my only option.


 3:12 pm on May 21, 2010 (gmt 0)

did you try setting the domain parameter of the setcookie function explicitly to the other domain?


 6:33 pm on May 21, 2010 (gmt 0)

Yes, that doesn't work either. From what I understand, the domain parameter on the setcookie function only applies to subdomains of the current domain. It seems there is no way to set a cookie for another domain even if you redirect to that domain, in the same script that you redirect in that is. Of course, the page redirected to could set the cookie, but that won't help me.

I think I'm just going to redirect with a url parameter and check the user agent and redirect spiders to a version without the parameter.


 8:25 pm on May 21, 2010 (gmt 0)

I don't know of any such limitation

setcookie("Redirect", "Yes", time()+60*60*24*365, "/", 'www.domain.com');

that should make it available to the other domain, though hidden from the first, haven't tested it myself but I believe I have done that in the past

on the other domain you should be able to dump $_COOKIE and see


 9:10 pm on May 21, 2010 (gmt 0)

I just spent a few minutes with a HTTP header checker and the problem is that the domain specified in the setcookie function is just ignored. It sets the cookie in the original domain regardless of whether you specify a domain in the function or not. It could very well be a function of the fact that I'm doing a 301 redirect. If I check out the 301 header, I can see that it's setting a cookie in the original domain.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved