当使用ajax将请求发送到服务器时,我无法检索$ _COOKIE ['mycookiename']的值

当使用ajax将请求发送到服务器时,我无法检索$ _COOKIE ['mycookiename']的值

问题描述:

I want to send something to server with ajax GET method and also using cookies, so I send my value to ajax, I need this value to update the current cookie value. In ajax file, I can't retrieve $_COOKIE['cookie_name'] , when I echo this, the result will be like this--> [] !

function card(action,value) {
    if (value == "") {
        document.getElementById('errmsgs').innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('errmsgs').innerHTML = xmlhttp.responseText;
            }
        };

        xmlhttp.open("GET","inc/pricing/pricing.php?act="+action+"&val="+value,true);
        xmlhttp.send();
    }
}

And the pricing page is:

<?php 
require_once '../../connection.php';
require_once '../../func.php';

$what_is_action = $_REQUEST['act']; 
$myCook = $_REQUEST['val'];

}
echo $full_cookie_value = $_COOKIE['mycookiename']; ----> output will be--> [] ---->but it must be something like this ---> {"wwde34":"1","effy33":"1","ssdfff":"1"}


if($what_is_action == 'delete'){
    //delete_cook($myCook);
}
?>

Solved.
The problem was, when I trying to create the cookie, it was not in root, so in another folders It can't be accessible! so I added '/' at the last parameter of setcookie for example: setcookie('a','b',time()+36666,'/') this will create cookie in root and it can be accessible in other folders!

Has your cookie been made?

Make sure the cookie is set! Example:

Where you see the I button you will need to press it and find the cookie and capsulses, check if you can find your cookie in there! This must be done from your website.

Getting the error

try
{
    echo $_COOKIE['name']['bills'];
}
catch (Throwable $t) // PHP 7
{
    echo '<b>COOKIE is not made</b>';
}
catch (Exception $e) // PHP 5
{
    echo '<b>COOKIE is not made</b>';
}

Please have a look at : Getting cookies with javascript

You are aware that, if that is the actual code from your file, that you're not assigning a variable after the echo, right?

echo full_cookie_value should be echo $full_cookie_value, or if you tried echoing 'full_cookie_value' as a "label", echo 'full_cookie_value '.$_COOKIE['mycookiename']