PHP会话未设置无法正常工作

时间:2016-08-08 06:03:47

标签: php session

<?php session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['submit']) && $_POST['submit'] == "submit")
{   
    $_SESSION['name'] = $_POST['Name'];
    $_SESSION['father_name'] = $_POST['Father_name'];
    $_SESSION['class'] = $_POST['Class'];
    $_SESSION['address'] = $_POST['Address'];

}
if(isset($_SESSION['name']) && isset($_SESSION['father_name']) && isset($_SESSION['class']) && isset($_SESSION['address'])) 
{       
    echo $_SESSION['name'] . '<br>';
    echo $_SESSION['father_name'] . '<br>';
    echo $_SESSION['class'] . '<br>';
    echo $_SESSION['address'] . '<br>';
?>
<form method="post" name="secondform">
<button type="submit" name="clear" value="clear">clear</button> 
</form>
<?php
    if (isset($_POST['clear']))
    {
        session_unset();    
                session_unset();        

    }       
} 
else 
{ ?>
    <form method="post" name="myform">
        <input type="text" name="Name" placeholder="Name"/>
        <input type="text" name="Father_name" placeholder="Father name"/>
        <input type="text" name="Class" placeholder="class"/>
        <input type="text" name="Address" placeholder="address"/>
        <button type="submit" name="submit" value="submit">subbmit</button>
    </form>
<?php } ?>
</body>
</html>

我需要在清除按钮上单击两次。循环不能正常工作,session_unset无法正常工作。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

session_unset()部分移到页面的开头,紧跟在session_start()之后(始终必须是第一个)。目前,点击&#34;清除&#34;首先回显存储的值,然后清除它们。您不必真正点击两次,清除后再次加载页面会显示会话为空。通过移动session_unset(),您可以在尝试回显之前清除它们。