setcookie forms the HTTP header in the response to the browser. The $_COOKIE takes values from the browser's request headers. Look carefully at how the HTTP protocol is arranged or any article about cookies and HTTP headers. Or just look in Wireshark for HTTP traffic of your code, everything is also very clear and clear there, if you know where to look
Simply put, the $_COOKIE["login"] cookie at the time of accessing it has not yet been defined. To be precise, in the $_COOKIE array, $_COOKIE is still no "login" key, and a call to a non-existent element will cause an error
Just check any value before using it:
$login = $_POST["cooklog"]; setcookie("login", $login, time() + (3600*24)*30, "/"); $xlogin = isset($_COOKIE["login"]) ? $_COOKIE["login"] : NULL; if ($xlogin) { echo json_encode($xlogin); } else { // Напишите, что делать, если логика в куках нет }
By the way, $_POST["cooklog"] also advisable to check before use. In general, all calls to arrays need to be checked, you are trying to work with what may not be, and create yourself a lot of problems for the future