简体   繁体   English

新的和改进的 PHP Session 重定向问题

[英]New & Improved PHP Session Redirect Problem

I'm working on a multi-page form.我正在处理一个多页表单。 Here's the outline of its function:这是它的function的轮廓:

Part One: Collect Form Input Fields -> Sends user to transfer.php using POST method第一部分:收集表单输入字段 -> 发送用户到 transfer.php 使用 POST 方法

Part Two: transfer.php |第二部分:转移。php | Validates that nothing is empty -> Stores $_POST variables into a $_SESSION -> Redirects user to next part of the form.验证没有什么是空的 -> 将 $_POST 变量存储到 $_SESSION -> 将用户重定向到表单的下一部分。

Part Three: Second Part of Form |第三部分:表格的第二部分 | Maintains the session from the first form -> Adds new variables to the session -> Preps for everything to get emailed维护第一个表单中的 session -> 将新变量添加到 session -> 准备所有内容以通过电子邮件发送

I've been reading how using header( location: ) etc drops session information.我一直在阅读使用标头(位置:)等如何删除 session 信息。 The solution people seem to find is to validate the form on page, then direct the user using the action field of the form.人们似乎找到的解决方案是验证页面上的表单,然后使用表单的操作字段指导用户。


The problem I'm having: Where the form redirects is based on the input from the form.我遇到的问题:表单重定向的位置基于表单的输入。 There are actually 8 potential forms that the user could be forwarded to depending on what they input during the first form.实际上有 8 个潜在的 forms 用户可以根据他们在第一个表单中输入的内容被转发到。

I've tried SID techniques and the SESSION variables show up as registered, but they all display &NULL.我已经尝试过 SID 技术,并且 SESSION 变量显示为已注册,但它们都显示 &NULL。

Question: How can I get the session variables to display on the next page?问题:如何让 session 变量显示在下一页上?


Here's the code I have so far:这是我到目前为止的代码:

Part 1: the form第 1 部分:表格

<form method="POST" action="transfer.php">
... all my form elements with proper names
</form>

Part 2: transfer.php第 2 部分:转移。php

<?php
session_start();
$_SESSION = $_POST;

// project_type would store an url for the corresponding 2nd part of the form
$redirect = $_SESSION['project_type'];
$redirect .= "?phpSESSID=".session_id();
header("Location: $redirect");
exit();
?>

Part 3: the next form第 3 部分:下一个表格

<?php
// initialize a session
session_start();
echo "<p>Session ID: " . session_id() . "</p>";                     
echo "<p>Vardump Set Off Page: ";
var_dump($_SESSION);
echo "</p>";
?>

<form method="...... the rest of the form etc

I know that the post is making it to transfer.php correctly.我知道该帖子正在正确地转移。php。 I know that the content of $_POST is being copied to $_SESSION correctly.我知道 $_POST 的内容被正确复制到 $_SESSION 。 I know that somewhere during the redirect, the session variables are being set to &NULL我知道在重定向期间的某处,session 变量被设置为 &NULL

Here's what the var_dump looks like after the user gets redirected to the 2nd part of the form.这是用户被重定向到表单的第二部分后 var_dump 的样子。

array(18) { ["project_type"]=> &NULL ["project_timeframe"]=> &NULL ["company_name"]=> &NULL ["company_address"]=> &NULL ["company_city"]=> &NULL ["company_state"]=> &NULL ["company_zipcode"]=> &NULL ["company_phone"]=> &NULL ["company_website"]=> &NULL ["contact_name"]=> &NULL ["contact_title"]=> &NULL ["contact_email"]=> &NULL ["contact_phone"]=> &NULL ["contact_primary_phone_type"]=> &NULL ["contact_phone_alt"]=> &NULL ["contact_alternate_phone_type"]=> &NULL ["contact_preferences"]=> &NULL ["contact_preferences_additional"]=> &NULL } 

Just use session_start();只需使用session_start(); at the top of the page.在页面顶部。
There is no need for the session_id($_GET['phpSESSID']);不需要session_id($_GET['phpSESSID']); unless you are changing to a different session.除非您更改为不同的 session。

EDIT:编辑:

Also in transfer.php you just want all the posts to be saved to the session so just do:同样在transfer.php中,您只想将所有帖子保存到 session 中,只需执行以下操作:

session_start();
$_SESSION = $_POST;

ALSO: remove this line: session_write_close();另外:删除这一行: session_write_close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM