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
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
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, changehttp://website2.example.com
to your website 2's domain.
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.