无法读取$ _SESSION值

时间:2016-01-15 19:30:10

标签: php session-cookies

我尝试创建一个按钮,删除现有的Cookie并使用此

保存其他数据
    <?php session_start(); ?>
<html>
    <head>
        <title>Reading Cookies</title>
    </head>
    <body>
        <?php // Reading the value of a cookie

            // give $var1 a default value
            $var1 = 'cookie not set';
            $var2 = 'unspecified';
            // if cookie with name 'test' exists then set $var1 to its value
            if (isset($_COOKIE['id'],$_COOKIE['usr'])) {
                $var1 = $_COOKIE['id'];
                $var2 = $_COOKIE['usr'];

            }
            echo "id= {$var1}<br/>usr= {$var2}";
        ?>

        <?php // Deleting a cookie

            // set cookie value to 0 and expiration to the distant past
            if(isset($_GET['logout'])) {
            setcookie('usr', null, time()-(60*60*24*7));
            setcookie('id', null, time()-(60*60*24*7));
            $_SESSION['first_name'] = "aa";
            $_SESSION['last_name'] = "bb";
            }
         ?>
         <br/>
         <a href="?logout">logout</a>

    </body>
</html>

但是当我尝试通过其他页面访问first_name和last_name valus

<?php session_start();
        // read values from the session
        $name = $_SESSION['first_name'] . " " . $_SESSION['last_name'];
        echo $name;
    ?>

它显示错误 undefined index first_name

0 个答案:

没有答案