简体   繁体   中英

New & Improved PHP Session Redirect Problem

I'm working on a multi-page form. Here's the outline of its function:

Part One: Collect Form Input Fields -> Sends user to transfer.php using POST method

Part Two: transfer.php | Validates that nothing is empty -> Stores $_POST variables into a $_SESSION -> Redirects user to next part of the form.

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

I've been reading how using header( location: ) etc drops session information. 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.

I've tried SID techniques and the SESSION variables show up as registered, but they all display &NULL.

Question: How can I get the session variables to display on the next page?


Here's the code I have so far:

Part 1: the form

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

Part 2: transfer.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

<?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. I know that the content of $_POST is being copied to $_SESSION correctly. I know that somewhere during the redirect, the session variables are being set to &NULL

Here's what the var_dump looks like after the user gets redirected to the 2nd part of the form.

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(); at the top of the page.
There is no need for the session_id($_GET['phpSESSID']); unless you are changing to a different session.

EDIT:

Also in transfer.php you just want all the posts to be saved to the session so just do:

session_start();
$_SESSION = $_POST;

ALSO: remove this line: session_write_close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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