0

I have a script that reads a cookie, but if it is not set writes a cookie. But, although cookie is set, sometimes the script returns several 'set cookie' in logs. I can not find why. Some idea? Thanks

Error log:
Get cookie H31X7P35K6R5CJP084LIAQQCD8N1H8I6
Set new cookie 3I8JW4GDTXZZS9FD2SA9OT1G07ATIYML
Cart cookie is 3I8JW4GDTXZZS9FD2SA9OT1G07ATIYML
Get cookie H31X7P35K6R5CJP084LIAQQCD8N1H8I6
Get cookie H31X7P35K6R5CJP084LIAQQCD8N1H8I6
Get cookie H31X7P35K6R5CJP084LIAQQCD8N1H8I6
Get cookie H31X7P35K6R5CJP084LIAQQCD8N1H8I6
add_action('init', 'cart_cookie', 10);

function cart_cookie(){
    $time = time() + 86400 * 7; seven days expiration date
    if ( !isset( $_COOKIE['cart_cookie'])){

       // set cookie
        $cookie_id = mis_cursos_random_str(32);
        @setcookie( 'cart_cookie', $cookie_id, ['samesite' =>'strict', 'expires' => $time , 'httponly'=>'true', 'path' =>'/']); // 
       
            error_log( 'Set new cookie '.$cookie_id);       

    } else {

        // get cookie
        $cookie_id = $_COOKIE['mis_cursos_cart_cookie'];                   error_log( 'Get cookie '.$cookie_id);
    } 
    return $cookie_id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.