有与该主题相关的类似问题,但没有一个解决了我的问题。有点奇怪,但是我的$ _SESSION在同一页面上工作,但不在其他页面上工作。如果我放置isset($ _ POST ['submit'),则条件不满足,没有它,$ _ SESSION将保持为空。
这是我的代码。
这是登录页面。
<!-- Login.php -->
<?php
session_start();
?>
<html>
<body>
<form method="post" action="profile.php">
<fieldset>
<legend>
Login
</legend>
<label> User ID :</label> <input type="text" placeholder="Username" name="user"><br>
<label> Password :</label> <input type="password" placeholder="Password" name="password">
<input type="submit" name="submit" value="Login">
</fieldset>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
?>
这是我希望显示会话变量的位置。
<!-- profile.php -->
<?php
session_start();
echo "Session user is ".$_SESSION['USER']."<br>";
unset($_SESSION['USER']);
unset($_SESSION['PASS']);
session_unset();
session_destroy();
?>
这是我尝试过的:
$_POST['submit']
和其余的post参数在Login.php
中不可用它们仅在profile.php
中可用,因为您的表单操作指向它。
您可以将以下代码移到session_start()
中的profile.php
之后。
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
请注意,您未在profile.php
的末尾设置会话值