-1

I have Following pseudo Code

<script>
  var a=fetching data which taking  much time; 
  document.cookie = "testing="+a;
</script>

<?php
$testing=$_COOKIE["testing"];
echo($testing);
?>

I m trying to get a single string value from Third party Api which taking almost 5 seconds using my internet . When I set timeout it work fine and save value in cookie. But when I run above code it. I does not saved the value and go to Php Scripting. I want that first Cookies should be set using Script than we should go to PHP

  • its work fine when I use set timeout function this is because of synchronous behavior of JavaScript. before fetch and save data in cookie from api. Php code runs – testing one Nov 24 '21 at 15:11
  • 1
    Go and have a good, thorough read of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CBroe Nov 24 '21 at 15:22

1 Answers1

1

Actually you should be able to access cookies with the superglobal $_COOKIE.

See https://www.php.net/manual/en/reserved.variables.cookies.php

What is important to know, you have to set the cookie with document.cookie so PHP can access this. window.cookie cannot be read by PHP.

But the cookie is only available on the next page reload and not on the first page state since it is initially set there. So maybe ajax is a possibility here to pass it to PHP, if you don't want to reload the page. It depends what you want to do.

Dennis
  • 183
  • 1
  • 1
  • 10
  • I m access cookies with the superglobal $_COOKIE. and have set the cookie with document.cookie in code.this was just pseudo code to make better understanding . but the problem is because of synchronous behavior of JavaScript. before fetch and save data in cookie from api. Php code runs. When I use Settimeout function in javascript.I get Value of cookie in Php – testing one Nov 24 '21 at 15:23