How set a cookie from PHP and get it without refresh

You need to use the ‘init’ hook similar to the following:

add_action( 'init', 'your_function');
function your_function() {
    setcookie('my_cookie','tmp_val');
    wp_redirect( get_permalink() ); 
        exit; 
    } 
}

You should add a conditional ‘if’ statement in there to prevent it redirecting on every page though!

eg.

   add_action( 'init', 'your_function');
    function your_function() {
        setcookie('my_cookie','tmp_val');
        if ($my_cookie == "???") { 
            wp_redirect( 'go here' ); 
            exit; 
            } else {
            wp_redirect( 'go somewhere else' ); 
            exit;
        } 
    }

Also consider the ‘template_redirect’ hook.