让 PHP COOKIE 立即生效(不用刷新就可以使用)

<?php
    function set_my_cookie($var, $value = '', $time = 0, $path = '', $domain = '') {
        $_COOKIE[$var] = $value;
        setcookie($var, $value, $time, $path, $domain);
    }
    // set_my_cookie('test', 'ryan_test');
    // echo $_COOKIE['test'];
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        set_my_cookie('test1', 'ryan_test1');
    }
    echo $_COOKIE['test1'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Cookie</title>
</head>
<body>
    <form action="" method='post'>
        <input type="submit" />
    </form>
</body>
</html>

 

posted @ 2018-06-23 14:38  Ryan_zheng  阅读(413)  评论(0编辑  收藏  举报