php - 无法在php中分配会话变量
问题描述
有与该主题相关的类似问题,但没有一个解决了我的问题。它有点奇怪,但我的 $_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();
?>
这是我尝试过的:
- 将表单方法更改为 GET。
- 使用 $_REQUEST 和 $_GET。
- 在同一页面上使用 $_SESSION。它在同一页面上工作。
- 检查会话 ID。其他页面上的会话存在,但值为 null 或空。
- 在没有 isset() 的情况下运行代码。在这种情况下,所有会话变量都保持为 NULL。
解决方案
$_POST['submit']
其余的 post 参数在它们中不可用,Login.php
它们仅在profile.php
因为您的表单操作指向它时才可用。
session_start()
您可以在in之后移动以下代码profile.php
。
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
请记住,您在结束时取消设置会话值profile.php
推荐阅读
- javascript - 如何使用 JavaScript 在 eml 中附加有效的 XLSX 文件
- javascript - 如何从另一个文件中的文件访问内容?
- python - 瓶子和蟒蛇。下载时更改静态文件的格式
- wordpress - Wordpress url 重写 add_rewrite_rule 类别、子类别、标签
- powershell - Powershell脚本将文件复制并粘贴到上一个目录
- java - 如何生成 4 个不重复数字的随机数
- couchdb - 如何使用过滤器从 couchdb 中删除文档?
- c# - 如何正确覆盖 System.Timers.Timer Elapsed Event
- navigation - SwiftUI NavigationLink 立即导航回来
- java - 由应用程序执行时的 Java Android Studio SQL ResultSet NullPointerException