【问题标题】:Session data not displaying会话数据不显示
【发布时间】: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>&nbsp;</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']?>&nbsp; </span>
            </div>      
            <div>
                <label>Favorite Century:</label>
                <span><?=$postedData['century']?>&nbsp; </span>
            </div>
            <div>
                <label>Comments:</label>
                <span><?=$postedData['comments']?>&nbsp; </span>
            </div>
            <div>
                <label>Name:</label>
                <span><?=$postedData['name']?>&nbsp; </span>
            </div>
            <div>
                <label>E-mail Address:</label>
                <span><?=$postedData['email']?>&nbsp; </span>
            </div>
            <div>
                <label>Receive Newsletter:</label>
                <span><?=$postedData['newsletter']?>&nbsp; </span>
            </div>
        </div>
    </body>
</html>

【问题讨论】:

  • 我会将include 'assets/include.php' 更改为require_once 'assets/include.php' 你可能有一堆通知级别错误.../调用会话开始多次。

标签: php


【解决方案1】:

您的表单似乎直接发布到final.php,因此您应该可以访问$_POST中发布的数据。

它不会设置为会话,因为index.php 中的代码没有运行(表单发布到另一个文件)

如果您想将数据设置到会话中,您应该在final.php 中这样做。

【讨论】:

  • 我不再包含 session_start();在 index.php 但 final.php 仍然显示空数组。数组 ( [formPostData] => 数组 ( ) )
  • 您应该像这样从$_POST 获取帖子数据:$postedData = $_POST
  • 谢谢你。我更新了问题中的代码以反映工作副本。
【解决方案2】:

根据您在 index.php 中的表单操作,您的表单将发布到 final.php ,您没有设置任何会话数据,因此您的会话数据是空数组。

如果您希望在 final.php 中打印会话数据,因此您必须在设置会话数据后将表单操作更改为 index.php,您需要重定向到 final.php 然后你会得到会话数据。

【讨论】:

  • 我是在 final.php 还是 index.php 中设置会话数据以便在 final.php 中打印?
  • 需要在final.php中设置会话
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2011-07-07
  • 2012-05-03
  • 1970-01-01
  • 2018-10-01
  • 2015-09-22
相关资源
最近更新 更多