php - 不能跨页面携带 SESSION 变量
问题描述
我在使用会话变量时遇到问题。我在 page1.php 上创建了会话变量,然后尝试在 page2.php 上显示它,但它不起作用。我确保添加 session_start(); 在页面的开头,所以这不是问题。
这是我的 page1.php 代码
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>website</title>
<link rel="icon" href="icon.png">
<link rel="stylesheet" type="text/css" href="style.css">
<?php
if(!isset($_POST["submit"])) {
$_SESSION["username"] = $_POST["username"];
}
?>
</head>
<body id="body">
<form action="page2.php" method="post">
<input type="username" id="username" name="username" placeholder="Username">
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
这是我的 page2.php 代码
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>website</title>
<link rel="icon" href="icon.png">
<link rel="stylsheet" type="text/css" href="style.css">
</head>
<body id="body">
Your username is: <?php echo $_SESSION["username"]; ?>
</body>
</html>
解决方案
$_POST['username']
将不可用page1.php
它只会在page2
响应提交的表格的页面中可用。
同样$_POST["submit"]
将不可用page1
在测试时将其添加到脚本的顶部,然后即使您正在为 LIVE 环境配置的站点上进行开发,您也会看到如下错误
应从其发送的未定义索引page1.php
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
推荐阅读
- python-3.x - LazyDocs - 生成的 *.md 文件不代表项目符号列表
- php - 调用未定义的方法 Illuminate\Database\Query\Builder::with() - Larvel
- selenium - 我们如何在 jmeter 中对 selenoid 运行 Selenium 测试?
- python - 为什么 Python 不喜欢与成员变量进行比较?
- sql - 使用 SQL 删除 Excel 工作表行
- gradle - 找不到 io.fabric.tools::gradle1.24.1
- arrays - 最大化可以由数组形成的所有非重叠子数组的最大和最小元素之间的绝对差之和?
- ethereum - 如何使用代理调用智能合约方法
- python - 用作索引的 Python 错误数组必须是整数(或布尔)类型
- python - 将等高线从一个图形投影到另一个图形