在第三页/最后一页中看不到我的会话变量?

时间:2016-03-12 17:06:57

标签: php macos

我在最后一页查看会话变量时遇到问题。用户应该能够提交名称,然后第二页应该要求您“单击此处”以查看最终页面中的名称或会话变量。 谢谢你的帮助!

1)第一页以表格提交姓名:

<form action="Cookies&SessionVariables2.php" method="post">
    <img src="question.png" alt="WhatIsYourAlias?">
    <input name="alias" size="60" type="text">
    <button id="button" type="submit">Submit</button>
</form>

2)第二页说“点击此处查看”:

<?php
//Handle the form if it has been submitted:
if (isset($_POST['alias'])){

    //Send the cookies:
    setcookie('alias',$_POST['alias']);
}
?>

<?php

//Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD']=='POST'){

    //Handle the form:
    if((!empty($_POST['alias']))){

        //Do session stuff
        session_start();
        $_SESSION['alias'] = 
        $_POST['alias'];

        //Message to be printed:
        echo $msg = '<p>Your Alias has been entered! Click <a href="Cookies&SessionVariables3.php">here</a> to view it!</p>';
     }

        //Forgot field
        else {
            print'<p>Awww! You did not submit your Alias! Please click <a href="Cookies&SessionVariables.php">here</a> and try again!</p>';
        } 
}     
?>

3)第三页应显示会话变量:

<?php

//Need the session:
session_start();

print '<p>Hello, ' . $_SESSION['alias'] . '!</p>';
?>

0 个答案:

没有答案