Saturday, 4 May 2019

Set cookie is have an issue with path in wordpress

I tried to set cookie on my Wordpress website.

So i write the following code on header.php

if($_REQUEST['my-key'] !==""){
        $value=$_REQUEST['my-key'];
        setcookie('new_my_code', $value, time() + (86400));
    }

But the problem of this is this cookie is only setting up with the corresponding page , not the entire domain .

For example if someone take http://bit.ly/2VG2ZCJ

then the cookie is set on /about path only . I want to set it on entire pages or entire domain , i tried many things nothing works

Try 1) header.php

    if($_REQUEST['my-key'] !==""){
        $value=$_REQUEST['my-key'];
        setcookie('new_my_code', $value, time() + (86400), '/');
    }



Try 2) header.php

    if($_REQUEST['my-key'] !==""){
        $value=$_REQUEST['my-key'];
        setcookie('new_my_code', $value, time() + (86400), '/', '.mywebsite.com');
    }



Try 3) header.php

    if($_REQUEST['my-key'] !==""){
        $value=$_REQUEST['my-key'];
        setcookie('new_my_code', $value, time() + (86400), COOKIEPATH, COOKIE_DOMAIN);
    }



Try 4) functions.php

    add_action( 'init', 'setting_my_first_cookie' );

    function setting_my_first_cookie() {
     if($_REQUEST['my-key'] !==""){
        $value=$_REQUEST['my-key'];
        setcookie('new_my_code', $value, time() + (86400), COOKIEPATH, COOKIE_DOMAIN);
    }
    }




5)

    $rp_path   = isset( $_SERVER['REQUEST_URI'] ) ? current( explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';
    setcookie('new_my_code', $value, time() + (86400),$rp_path, COOKIE_DOMAIN, is_ssl(), true);

I tired most things that i found on web to make it work . But whatever i am doing cookie is only setting on the same page . So please help

Is there any solution ? Is there any jQuery Solution ?



from Set cookie is have an issue with path in wordpress

No comments:

Post a Comment