Rasoul Zabihi Rasoul Zabihi - 1 year ago 75
Javascript Question

How to set a cookie for another domain

Say I have a website called

, and when a specific page of this site is loaded, say page link, I like to set a cookie for another site called
, then redirect the user to

I mean, on load of
I want to set a cookie for
and redirect user to

I tested it, and browser actually received the cookie from
, but it didn't send that cookie on the redirection request to
. Is it normal?

Can we set cookies for other domains?

Answer Source

You cannot set cookies for another domain. Allowing this would present an enormous security flaw.

You need to get b.com to set the cookie. If a.com redirect the user to b.com/setcookie.php?c=value

The setcookie script could contain the following to set the cookie and redirect to the correct page on b.com

    setcookie('a', $_GET['c']);
    header("Location: b.com/landingpage.php");