0

我在维护会话中的变量时遇到问题。我已经确认相同的会话 ID 通过页面导航持续存在,并且我还使用Print_r($_SESSION)来监视变量。

我正在使用四页。

  • 索引.php
  • 客户信息.php
  • custbilling.php
  • 确认.php

在第一页上,我使用表单将数据发送到下一页。

        <form name="prescreen" action="custinfo.php" method="post">
        <label>From DIA</label>
        <input name="startlocation" id="fromdia" type="radio" value="From DIA">
        <label>To DIA</label>
        <input name="startlocation" id="todia" type="radio" value="To DIA">
        <label>Choose Location:</label>
        City:<input name="city" id="city" type="text" />
        <span>or</span><br />
        Zipcode:<input name="zipcode" id="zipcode" type="text" />
    <h3>When do you need picked up?</h3>
        <label>Choose Date:</label>
        <input name="date" id="date" type="datetime-local" />
        <label>Choose Time:</label>
        <input name="time" id="time" type="time" />
    <input type="submit"  value="Get a Ride Now!" class="textbtn"></input>
    </form>

在 custinfo.php 上,然后我在文档的头部使用它:

 <?php
    session_start();

    $_SESSION['testvar'] = 'THIS IS A TEST';
    $startlocation = $_POST["startlocation"];
    $city = $_POST["city"];
    $zipcode = $_POST["zipcode"];
    $date = $_POST["date"];
    $time = $_POST["time"];

    //Assign variables to the Session
    $_SESSION['startlocation'] = $startlocation;
    $_SESSION['city'] = $city;
    $_SESSION['zipcode'] = $zipcode;
    $_SESSION['date'] = $date;
    $_SESSION['time'] = $time;

?>

变量被正确读入并存储在数组中。然后我在 custinfo.php 页面中使用这个表单:

        <form name="customerinfo" action="custbilling.php" method="post">
        <label>Contact Name</label>
        <input name="contactname" id="contactname" type="text">
        <label>Contact Email</label>
        <input name="contactemail" id="contactemail" type="text">
        <label>Contact Phone</label>
        <input name="contactphone" id="contactphone" type="text" />
        <?php
        if($_SESSION['startlocation'] == "From DIA")
        {
            echo '<hr />';
            echo '<label><b>To Location:</b></label>';
            echo 'Address1:<input name="toaddress1" id="toaddress1" type="text" />';
            echo 'Address2:<input name="toaddress2" id="toaddress2" type="text" />';
            echo 'City:<input name="tocity" id="tocity" type="text" />';
            echo 'Zip:<input name="tozip" id="tozip" type="text"  />';
            echo '<hr />';
        }
        else
        {
            echo '<hr />';
            echo '<label><b>From Location</b></label>';
            echo 'Address1:<input name="fromaddress1" id="fromaddress1" type="text" />';
            echo 'Address2:<input name="fromaddress2" id="fromaddress2" type="text" />';
            echo 'City:<input name="fromcity" id="fromcity" type="text" />';
            echo 'Zip:<input name="fromzip" id="fromzip" type="text" />';
            echo '<hr />';
        }

        ?>

        <input type="submit"  value="Book Your Ride!" class="textbtn"></input>
        </form>

在 custbilling.php 页面的顶部,我将所有内容都拉进来,如下所示:

<?php
session_start();

/*Vars from Customer Info */
$contactname = $_POST['contactname'];
$contactemail = $_POST['contactemail'];
$contactphone = $_POST['contactphone'];
if($_SESSION['startlocation'] == "To DIA")
{
    $address1 = $_POST['fromaddress1'];
    $address2 = $_POST['fromaddress2'];
    $city = $_POST['fromcity'];
    $zipcode = $_POST['fromzip'];
}
else
{
    $address1 = $_POST['toaddress1'];
    $address2 = $_POST['toaddress2'];
    $city = $_POST['tocity'];
    $zipcode =$_POST['tozip'];
}

//Assign Variables to the Session
$_SESSION['contactname'] = $contactname;
$_SESSION['contactemail'] = $contactemail;
$_SESSION['contactphone'] = $contactphone;
$_SESSION['address1']=$address1;
$_SESSION['address2']=$address2;
$_SESSION['city']= $city;
$_SESSION['zipcode'] = $zipcode;

?>

此时,我正在显示如下信息:\

<h1><?php echo $_SESSION['testvar']; ?></h1>
    <h2>Travel Information</h2>
    <h3>Please fill out this form:</h3>
    <p>Direction of Travel: <?php echo $_SESSION['startlocation']; ?></p>
    <p>LocationTo: <?php echo $_SESSION['city'] , $_SESSION['zipcode']; ?></p>
    <p>Date: <?php echo $_SESSION['date']; ?></p>
    <p>Time: <?php echo $_SESSION['time']; ?></p>
    <p>Customer Name: <?php echo $_SESSION['contactname']; ?></p>
    <p>Customer Email: <?php echo $_SESSION['contactemail']; ?></p>
    <p>Customer Phone: <?php echo $_SESSION['contactphone']; ?></p>
    <p>Address Information: <br />
        <span>Address 1:</span><?php echo $_SESSION['address1']; ?><br />
        <span>Address 2:</span><?php echo $_SESSION['address2']; ?><br />
        <span>City:</span><?php echo $_SESSION['city']; ?><br />
        <span>ZipCode:</span><?php echo $_SESSION['zipcode']; ?><br />
    </p>

但是现在保存在数组中的 startlocation 和第一篇文章中的所有变量都被删除了。您还可以看到我用来测试会话是否正常工作的测试变量,以及我$_SESSION['testvar']在所有文件上的正确显示。我最初的代码集是这样的$_SESSION['varname] = $_POST['varname'];,但这产生了同样的问题。所以变量会转到下一页,但不会继续到第三页。

任何帮助将不胜感激。谢谢你。

编辑:这可能是有用的信息:

Result from custinfo.php : bs2rrqoo5u1u5mjerkg54nkcb1 Array ( [startlocation] => From DIA [city] => Denver [zipcode] => [date] => 11/27/2012 [time] => 09:00 [fromaddress1] => [fromaddress2] => [fromcity] => [fromzip] => [toaddress1] => [toaddress2] => [tocity] => [tozip] => [contactname] => [contactemail] => [contactphone] => [address1] => [address2] => [testvar] => THIS IS A TEST [sameaddress] => 1 [billaddress1] => [billaddress2] => [billcity] => [billzip] => [ccnumber] => [ccexp] => [ccsc] => )

Result from custbilling.php : bs2rrqoo5u1u5mjerkg54nkcb1 Array ( [startlocation] => [city] => Aurora [zipcode] => 80017 [date] => [time] => [fromaddress1] => [fromaddress2] => [fromcity] => [fromzip] => [toaddress1] => [toaddress2] => [tocity] => [tozip] => [contactname] => Elijah Gartin [contactemail] => elijah.gartin@gmail.com [contactphone] => 3038804117 [address1] => 124 Test [address2] => [testvar] => THIS IS A TEST [sameaddress] => 1 [billaddress1] => [billaddress2] => [billcity] => [billzip] => [ccnumber] => [ccexp] => [ccsc] => )
4

3 回答 3

1

Conflicting sessions result in null values.

Try using global variables or communicating the values through a persistant data structure.

于 2012-11-14T04:08:22.783 回答
0

问题出在服务器上。我不得不添加session.save_path = "/var/location"到 php.ini 文件中。感谢您的尝试。

于 2012-11-14T22:52:58.087 回答
0

从 http 到 https 会话变量将无法生存。

于 2012-11-13T17:59:00.177 回答