【问题标题】:Can't get information from the $_SESSION from a simple newbie form无法通过简单的新手表单从 $_SESSION 获取信息
【发布时间】:2010-02-24 12:38:13
【问题描述】:

在一种形式中,我向用户询问他的姓名,我使用 POST 并将用户发送到 FormTwo.php 并将 $_POST["name"] 保存到会话中。

在第二个中,我询问他的姓氏并做同样的事情并将他发送到 registerComplete.php 页面。

我正在尝试回应他写的所有信息,据我了解,这些信息应该可以通过 $_SESSION 数组访问,对吧?

我做错了什么? :)

RegisterFormOne.php:

<?php session_start(); ?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 1 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>
        <form action="registerFormTwo.php" method="post">
            <dt>First Name:</dt>
                <dd><input type="text" name="firstName" /></dd><br />

            <dt>Last Name:</dt>
                <dd><input type="text" name="lastName" /></dd><br />

            <dt>Age:</dt>
                <dd><input type="text" name="age" /></dd><br />

            <dt>Date of Birth:</dt>
                <dd><input type="text" name="dateOfBirth" /></dd><br />

            <dt>Gender:</dt>
                <dd>Masculino <input type="radio" value="M" name="gender" checked/> &nbsp;&nbsp;
                Femenino <input type="radio" value="F" name="gender" />
                </dd>
            <dt><input type="submit" /></dt>
        </form>
    </body>
</html>

RegisterFormTwo.php

<?php
if ($_POST){
  $_SESSION["firstName"] = $_POST["firstName"];
  $_SESSION["lastName"] = $_POST["lastName"];
  $_SESSION["age"] = $_POST["age"];
  $_SESSION["dateOfBirth"] = $_POST["dateOfBirth"];
  $_SESSION["gender"] = $_POST["gender"];
}
?>
<html>
    <head>
        <title>Registration Form - 2 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 2 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>               

        <form action="registerFinish.php" method="post">
            <dt>Nationality:</dt>
                <dd><input type="text" name="nationality" /></dd><br />

            <dt>Profession:</dt>
                <dd>
                    <select name="profession">
                        <option value="sistemas" selected="selected">Ing. Sistemas</option>
                        <option value="electrico">Ing. Electrico</option>
                        <option value="marketing">Marketing y Publicidad</option>
                        <option value="comercio">Comercio</option>
                    </select>
                </dd><br />

            <input type="submit" />
        </form>
    </body>
</html>

registerFinish.php

<?php
if ($_POST){
  $_SESSION["nationality"] = $_POST["nationality"];
  $_SESSION["profession"] = $_POST["profession"];
}
?>
<html>
    <head>
        <title>Registration Complete</title>
    </head>

    <body>
        <h1>Thank you for taking the census.</h1>
        <p>Please take a moment to review your inputted information and confirm when you feel everthing is OK.</p>               

        <ul>
            <li><?php /*This should echo his first name, but nothing shows. */ echo $_SESSION["firstName"]; ?></li>

        </ul>

    </body>
</html>

【问题讨论】:

    标签: php session


    【解决方案1】:

    你错过了其他2个文件开头的session_start();

    【讨论】:

    • 使用 session_start() 是否会重置保存的信息?
    • 不,它没有,它恢复当前的。来自 php.net “session_start() 根据通过 GET 或 POST 请求或通过 cookie 传递的会话标识符创建会话或恢复当前会话。”。
    • 不,把会话想象成一个全局数组,可以在调用session_start时使用
    • 不,会话在您的硬盘和 session_start() 中保存为一个特殊的 cookie;只是说你想使用它。
    【解决方案2】:

    必须使用session_start() 启动会话,每次执行要使用$_SESSION 的脚本。

    在这里,您有 3 个不同的脚本,它们通过 3 个不同的 HTTP 请求执行...
    这意味着应该在 3 个脚本的开头调用 session_start(),而不仅仅是第一个。


    作为参考,您可以查看手册的Session Handling 部分。

    【讨论】:

      【解决方案3】:

      RegisterFormTwo.php 和 registerFinish.php 在文件开头需要session_start();

      【讨论】:

        【解决方案4】:

        您必须在访问 $_SESSION 变量之前使用 session_start() ^^

        【讨论】:

          【解决方案5】:

          除了您的问题之外,如果您执行以下操作会更容易。请注意,我故意将表单放在会话中的数组中,因为它会自动填充,并且新的表单提交将刷新值(您想要的)。

          $_SESSION['form'] = array();    // flushes all the stored form collection in session 
          foreach ( $_POST as $key => $value )
          {
              $_SESSION['form'][$key] = $value;
          }
          
          // now you can access your form's "name" field via $_SESSION['form']['name']
          

          【讨论】:

            【解决方案6】:
            session_start();
            

            不会清除您的会话数据。会话数据只会在您清除时清除

            session_unset();
            session_destroy(); 
            

            或单个会话变量

            unset( $_SESSION[ 'yoursessionvar' ] );
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-12
              • 1970-01-01
              • 1970-01-01
              • 2017-12-08
              • 1970-01-01
              • 2015-10-02
              相关资源
              最近更新 更多