Rasoul Zabihi Rasoul Zabihi - 4 months ago 34
Javascript Question

How to set a cookie for another domain

Say I have a website called

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

I mean, on load of
a.com/link
I want to set a cookie for
b.com
and redirect user to
b.com
.

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

Can we set cookies for other domains?

Answer

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

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