2012-11-13 159 views
0

我在維護會話中的變量時遇到了問題。我已確認,通過頁面導航會保留相同的會話ID,並且我還使用Print_r($_SESSION)來監視變量。PHP變量被覆蓋爲空

我正在使用四頁。

  • 的index.php
  • custinfo.php
  • custbilling.php
  • confirm.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和所有變量現在已被刪除。您還可以看到我用來測試會話正在工作的testvariable,並且我的$_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] => [email protected] [contactphone] => 3038804117 [address1] => 124 Test [address2] => [testvar] => THIS IS A TEST [sameaddress] => 1 [billaddress1] => [billaddress2] => [billcity] => [billzip] => [ccnumber] => [ccexp] => [ccsc] =>) 

回答

0

問題出在服務器上。我不得不將session.save_path = "/var/location"添加到php.ini文件中。感謝嘗試的人。

0

Session變量將無法生存從http去到https。

+0

我不明白當我的一個變量在整個時間內都能存活時,情況如何?或者你說這個問題是與index.php?如果是的話,我假設我需要找出一種方法來強制index.php具有https? 實際上,目前沒有任何頁面正在https下......我強制索引使用https,並且所有地獄都打破了。 (因爲我沒有配置SSL證書) – ElijahGartin

1

衝突會話導致null值。

嘗試使用全局變量或通過持久數據結構傳遞值。

+0

你的意思是衝突會話究竟是什麼意思?每個部分的會話ID是相同的,所以我不確定這是否被忽略?我試圖避免使用全局變量,但當您說「通過持久數據結構傳遞值」時,您是什麼意思? – ElijahGartin