【发布时间】:2019-07-19 04:02:40
【问题描述】:
我有一个页面检查 $_POST 是否已设置,然后将发布数据保存到会话变量 $_SESSION。当页面重定向时,它不会在结果页面上显示会话数据。当我执行 print_r($_SESSION) 时,它显示一个空数组。在每个页面上,我都包含一个名为 include.php 的文件。在该页面中,我执行以下操作
<?php session_start(); ?>
index.php
<?php
include 'assets/include.php';
require 'assets/dbinfo.php';
$query = "SELECT * FROM Authors ORDER BY first_name";
$resultObj = $connection->query($query);
if(count($_POST))
{
$_SESSION['formPostData'] = $_POST;
header('Location: final.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Fundamentals</title>
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Header">
<img src="assets/Dickens_Gurney_head.jpg" border="0" alt="">
<h2>
Join Our Literature Mailing List
</h2>
</div>
<div id="Body">
<form method="post" action="index.php" >
<div>
<label>Favorite Author:</label>
<select name="author">
<?php while($row = $resultObj->fetch_assoc()): ?>
<option value="<?=$row['id']?>"><?=$row['first_name']?> <?=$row['last_name']?></option>
<?php endWhile; ?>
</select>
</div>
<div class="multiple">
<label>Favorite Century:</label>
17th Century <input type="checkbox" name="century[]" value="17th">
18th Century <input type="checkbox" name="century[]" value="18th">
19th Century <input type="checkbox" name="century[]" value="19th">
</div>
<div>
<label>Comments:</label>
<textarea name="comments"></textarea>
</div>
<div>
<label>Name:</label>
<input type="text" name="name" />
</div>
<div>
<label>E-mail Address:</label>
<input type="text" name="email" />
</div>
<div class="multiple">
<label>Receive Newsletter:</label>
Yes <input type="radio" name="newsletter" value="yes">
No <input type="radio" name="newsletter" value="no">
</div>
<div class="multiple">
<label> </label>
<input type="submit" name="submit" value="Join Mailing List">
</div>
</form>
</div>
</body>
</html>
final.php
<?php
include 'assets/include.php';
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
$postedData = $_SESSION['formPostData'];
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Fundamentals</title>
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Header">
<img src="assets/Dickens_Gurney_head.jpg" border="0" alt="">
<h2>
Mailing List Information
</h2>
</div>
<div id="Body">
<div>
<label>Favorite Author:</label>
<span><?=$postedData['author']?> </span>
</div>
<div>
<label>Favorite Century:</label>
<span><?=$postedData['century']?> </span>
</div>
<div>
<label>Comments:</label>
<span><?=$postedData['comments']?> </span>
</div>
<div>
<label>Name:</label>
<span><?=$postedData['name']?> </span>
</div>
<div>
<label>E-mail Address:</label>
<span><?=$postedData['email']?> </span>
</div>
<div>
<label>Receive Newsletter:</label>
<span><?=$postedData['newsletter']?> </span>
</div>
</div>
</body>
</html>
【问题讨论】:
-
我会将
include 'assets/include.php'更改为require_once 'assets/include.php'你可能有一堆通知级别错误.../调用会话开始多次。
标签: php