php - Problem in Accessing third-party cookies from browser when the laravel project is uploaded on a shared hosting

I created a two simple website in a shared hosting where one can create cookies and another one where it can supposedly get that created cookie, although the first one is creating the cookie the second website cannot get the corresponding data of that cookie. Both website are subdomains name

website 1 to create cookie

public function setCookie(Request $request) {
  $minutes = 60;
  $response = new Response('Created Cookie');
  $response->withCookie(cookie('name', 'userName', $minutes));
  return $response;
}

website 2 to get cookie

public function getCookie(Request $request) {
  $value = $request->cookie('name');
  echo $value;
}

I've tried change the value in $request->cookie('name') and change the name to 'laravel_session' just to check if its working and when I tried that, I could get the data of that cookie. I'm not really sure why its not working. Thank you

Answer

Solution:

If both of these websites have a different subdomain, then the browser by default prevents access to another website's cookies for security reasons.

You can allow website 2 to get website 1's cookies by adding the following HTTP headers inside website 1:

    header("Access-Control-Allow-Origin: http://website2.example.com");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: GET, POST");
    header("Access-Control-Allow-Headers: Content-Type, *");

of course, change http://website2.example.com to your website 2's domain.

Answer

Solution:

Laravel encrypts cookies by default. You need to create shared un-encrypted cookie to handle the issue. Follow this guide: https://gist.github.com/JacobBennett/15558410de2a394373ac

Source