2011-01-22 125 views
-1

更新解決的問題!PHP會話問題

檢查代碼後,再用問題是,我錯過了}else{}

header ('location:referraldone.php?say=blankfields'); 
} 


我真的迷失在這個會議代碼的時刻。我不明白爲什麼會議不起作用。我哪裏做錯了?當有人發出錯誤而未填寫必填字段時,我正嘗試將字段中輸入的數據轉到下一頁。代碼如下:

<?php 

if (!$_POST['cname'] | !$_POST['cphone'] | !$_POST['caddress'] | !$_POST['ccity'] | !$_POST['cstate'] | !$_POST['czip'] | !$_POST['cbirthday']) { 

$rname = $_REQUEST['rname']; 
$rdate = $_REQUEST['rdate']; 
$ragency = $_REQUEST['ragency']; 
$rphone = $_REQUEST['rphone']; 
$cname = $_REQUEST['cname']; 
$cphone = $_REQUEST['cphone']; 
$caddress = $_REQUEST['caddress']; 
$ccity = $_REQUEST['ccity']; 
$cstate = $_REQUEST['cstate']; 
$czip = $_REQUEST['czip']; 
$cbirthday = $_REQUEST['cbirthday']; 
$medmemid = $_REQUEST['medmemid']; 
$medclaim = $_REQUEST['medclaim']; 
$marital = $_REQUEST['marital']; 
$income = $_REQUEST['income']; 
$gender = $_REQUEST['gender']; 
$race = $_REQUEST['race']; 
$caregiver = $_REQUEST['caregiver']; 
$relationship = $_REQUEST['relationship']; 
$memphone = $_REQUEST['memphone']; 
$physician = $_REQUEST['physician']; 
$phyphone = $_REQUEST['phyphone']; 

session_start(); 
$_SESSION['rname'] = $rname; 
$_SESSION['rdate'] = $rdate; 
$_SESSION['ragency'] = $ragency; 
$_SESSION['rphone'] = $rphone; 
$_SESSION['cname'] = $cname; 
$_SESSION['cphone'] = $cphone; 
$_SESSION['caddress'] = $caddress; 
$_SESSION['ccity'] = $ccity; 
$_SESSION['cstate'] = $cstate; 
$_SESSION['czip'] = $czip; 
$_SESSION['cbirthday'] = $cbirthday; 
$_SESSION['medmemid'] = $medmemid; 
$_SESSION['medclaim'] = $medclaim; 
$_SESSION['marital'] = $marital; 
$_SESSION['income'] = $income; 
$_SESSION['gender'] = $gender; 
$_SESSION['race'] = $race; 
$_SESSION['caregiver'] = $caregiver; 
$_SESSION['relationship'] = $relationship; 
$_SESSION['memphone'] = $memphone; 
$_SESSION['physician'] = $physician; 
$_SESSION['phyphone'] = $phyphone; 

header ('location:referraldone.php?say=blankfields'); 
} 

?> 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <table style="text-align:left; width: 500px; left:45px; position:relative; padding:2px; border:1px solid #1A1A1A"> 
    <tr> 
     <td colspan="2" style="vertical-align:top; text-align:center; border:1px solid #1A1A1A; background-color:#A52A2A; color:#FFFFFF;">REFERRAL FORM</td> 
    </tr> 
    <tr> 
    <td colspan="2"><center>Choose us as your Home Care Agency!<br />We provide services 24 hours per day, 7 days a week.<br /><br />Please submit form below and your respond time<br />will be 48 hours.<br /><br /><strong>*Bold Fields Required</strong><br /><br /></td> 
    </tr> 
    <tr> 
     <td valign="top">Reffered By:<br /><input name="rname" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Reffered Date:<br /><input name="rdate" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Reffered Agency:<br /><input name="ragency" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="rphone" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Client Name:</strong><br /><input name="cname" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Phone:</strong><br /><input name="cphone" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Address:</strong><br /><input name="caddress" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* City:</strong><br /><input name="ccity" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* State:</strong><br /><input name="cstate" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Zip Code:</strong><br /><input name="czip" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Birthday:</strong><br /><input name="cbirthday" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Medicaid Member ID:<br /><input name="medmemid" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Medicare Claim:<br /><input name="medclaim" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Marital Status:<br /><input name="marital" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Monthly Income:<br /><input name="income" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Gender:<br /><input name="gender" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Race:<br /><input name="race" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Caregiver Name:<br /><input name="caregiver" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Relationship:<br /><input name="relationship" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="memphone" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Physician:<br /><input name="physician" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="phyphone" type="text" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top" style="color:#C00000"><br />Please make sure all the <strong>BOLD</strong> fields are completed before submitting form.<br /><br /></td> 
    </tr> 
    <tr> 
     <td align="right"><input type="reset" value="Clear Fields"> <input type="submit" name="send" value="Send" /></td> 
    </tr> 
    </table> 
</form> 

這裏是一個應該採取的會話數據,並用它滿山遍野的錯誤頁面,但是它沒有:

<?php 
$sb = "5"; 

$say = $_REQUEST['say']; 

$rname = $_SESSION['rname']; 
$rdate = $_SESSION['rdate']; 
$ragency = $_SESSION['ragency']; 
$rphone = $_SESSION['rphone']; 
$cname = $_SESSION['cname']; 
$cphone = $_SESSION['cphone']; 
$caddress = $_SESSION['caddress']; 
$ccity = $_SESSION['ccity']; 
$cstate = $_SESSION['cstate']; 
$czip = $_SESSION['czip']; 
$cbirthday = $_SESSION['cbirthday']; 
$medmemid = $_SESSION['medmemid']; 
$medclaim = $_SESSION['medclaim']; 
$marital = $_SESSION['marital']; 
$income = $_SESSION['income']; 
$gender = $_SESSION['gender']; 
$race = $_SESSION['race']; 
$caregiver = $_SESSION['caregiver']; 
$relationship = $_SESSION['relationship']; 
$memphone = $_SESSION['memphone']; 
$physician = $_SESSION['physician']; 
$phyphone = $_SESSION['phyphone']; 

if(isset($say)){ 
    switch ($say){ 
     case "formsent": 
     $return = "Referral Form Sent. Your respond time will be 48 hours."; 
     break; 
     case "blankfields": 
     $return = "All <strong>Bold</strong> Fields Required"; 
     break; 
     default:  
     break; 
    } 
} 

if (isset($_POST['send'])) { 

    if (!$_POST['cname'] | !$_POST['cphone'] | !$_POST['caddress'] | !$_POST['ccity'] | !$_POST['cstate'] | !$_POST['czip'] | !$_POST['cbirthday']) { 

$rname = $_REQUEST['rname']; 
$rdate = $_REQUEST['rdate']; 
$ragency = $_REQUEST['ragency']; 
$rphone = $_REQUEST['rphone']; 
$cname = $_REQUEST['cname']; 
$cphone = $_REQUEST['cphone']; 
$caddress = $_REQUEST['caddress']; 
$ccity = $_REQUEST['ccity']; 
$cstate = $_REQUEST['cstate']; 
$czip = $_REQUEST['czip']; 
$cbirthday = $_REQUEST['cbirthday']; 
$medmemid = $_REQUEST['medmemid']; 
$medclaim = $_REQUEST['medclaim']; 
$marital = $_REQUEST['marital']; 
$income = $_REQUEST['income']; 
$gender = $_REQUEST['gender']; 
$race = $_REQUEST['race']; 
$caregiver = $_REQUEST['caregiver']; 
$relationship = $_REQUEST['relationship']; 
$memphone = $_REQUEST['memphone']; 
$physician = $_REQUEST['physician']; 
$phyphone = $_REQUEST['phyphone']; 

session_start(); 
$_SESSION['rname'] = $rname; 
$_SESSION['rdate'] = $rdate; 
$_SESSION['ragency'] = $ragency; 
$_SESSION['rphone'] = $rphone; 
$_SESSION['cname'] = $cname; 
$_SESSION['cphone'] = $cphone; 
$_SESSION['caddress'] = $caddress; 
$_SESSION['ccity'] = $ccity; 
$_SESSION['cstate'] = $cstate; 
$_SESSION['czip'] = $czip; 
$_SESSION['cbirthday'] = $cbirthday; 
$_SESSION['medmemid'] = $medmemid; 
$_SESSION['medclaim'] = $medclaim; 
$_SESSION['marital'] = $marital; 
$_SESSION['income'] = $income; 
$_SESSION['gender'] = $gender; 
$_SESSION['race'] = $race; 
$_SESSION['caregiver'] = $caregiver; 
$_SESSION['relationship'] = $relationship; 
$_SESSION['memphone'] = $memphone; 
$_SESSION['physician'] = $physician; 
$_SESSION['phyphone'] = $phyphone; 

header ('location:referraldone.php?say=blankfields'); 
} 
?> 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <table style="text-align:left; width: 500px; left:45px; position:relative; padding:2px; border:1px solid #1A1A1A"> 
    <tr> 
     <td colspan="2" style="vertical-align:top; text-align:center; border:1px solid #1A1A1A; background-color:#A52A2A; color:#FFFFFF;">REFERRAL FORM</td> 
    </tr> 
    <tr> 
    <td colspan="2"><center>Choose us as your Home Care Agency!<br />We provide services 24 hours per day, 7 days a week.<br /><br />Please submit form below and your respond time<br />will be 48 hours.<br /><br /><strong>*Bold Fields Required</strong><br /><br /></td> 
    </tr> 
    <tr> 
     <td valign="top">Reffered By:<br /><input name="rname" type="text" value="<?php echo $rname; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Reffered Date:<br /><input name="rdate" type="text" value="<?php echo $rdate; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Reffered Agency:<br /><input name="ragency" type="text" value="<?php echo $ragency; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="rphone" type="text" style=value="rphone" "width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Client Name:</strong><br /><input name="cname" type="text" value="<?php echo $cname; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Phone:</strong><br /><input name="cphone" type="text" value="<?php echo $cphone; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Address:</strong><br /><input name="caddress" type="text" value="<?php echo $caddress; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* City:</strong><br /><input name="ccity" type="text" value="<?php echo $ccity; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* State:</strong><br /><input name="cstate" type="text" value="<?php echo $cstate; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Zip Code:</strong><br /><input name="czip" type="text" value="<?php echo $czip; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br /><strong>* Birthday:</strong><br /><input name="cbirthday" type="text" value="<?php echo $cbirthday; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Medicaid Member ID:<br /><input name="medmemid" type="text" value="<?php echo $medmemid; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Medicare Claim:<br /><input name="medclaim" type="text" value="<?php echo $medclaim; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Marital Status:<br /><input name="marital" type="text" value="<?php echo $marital; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Monthly Income:<br /><input name="income" type="text" value="<?php echo $income; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Gender:<br /><input name="gender" type="text" value="<?php echo $gender; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Race:<br /><input name="race" type="text" value="<?php echo $race; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Caregiver Name:<br /><input name="caregiver" type="text" value="<?php echo $caregiver; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Relationship:<br /><input name="relationship" type="text" value="<?php echo $relationship; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="memphone" type="text" value="<?php echo $phone; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Physician:<br /><input name="physician" type="text" value="<?php echo $physician; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top"><br />Phone:<br /><input name="phyphone" type="text" value="<?php echo $phyphone; ?>" style="width:98%" /></td> 
    </tr> 
    <tr> 
     <td valign="top" style="color:#C00000"><br />Please make sure all the <strong>BOLD</strong> fields are completed before submitting form.<br /><br /></td> 
    </tr> 
    <tr> 
     <td align="right"><input type="reset" value="Clear Fields"> <input type="submit" name="send" value="Send" /></td> 
    </tr> 
    </table> 
</form> 
+1

你在開玩笑吧?像這樣發佈代碼**的**牆並不是提問的有用方法。把它放到顯示問題的最小代碼。 – 2011-01-22 01:52:15

+0

呃......這段代碼是問題所在。會話由於某種原因沒有註冊。請檢查它。 – yanike 2011-01-22 01:56:28

+1

對不起@yanike,這需要基本的調試和一個更詳細的錯誤說明。究竟什麼不行,在哪一點? – 2011-01-22 01:57:39

回答

1

好了,一些重寫幫助將無法進行自動的,甚至沒有。

我最終將首先製作一個字段名稱和標題的描述性數組。通過這種方式,您可以獲得可接受的輸入字段的安全列表,並在以後簡化輸出。

$allowed_fields = array(" 
     "cname" => "* Client name", // titles may include <b>HTML</b> 
     "phone" => "PHone number", 
     "caddress" => "Address", 
     ... 
"); 

但簡化的第一個腳本,您可以代替$ _REQUEST輸入的50線和$ _SESSION只需填寫:

session_start(); 
foreach ($_REQUEST as $field=>$value) { 
    $_SESSION[$field] = $_REQUEST[$field]; 
} 

而且所有表單域輸出可以簡化這個方式:

foreach ($allowed_fields as $field=>$title) { 
    ?><tr> 
    <td valign="top"><?=$title?>:<br /><input name="<?=$field?>" type="text" value="<?=htmlspecialchars($_SESSION[$field])?>" style="width:98%" /></td> 
    </tr><?php 
} 
3

幾個明顯的問題與您的代碼:

  • if子句中,您正在使用二進制或|而不是布爾值或||
  • 而不是手動本地化變量,您可能還需要extract($_REQUEST); - 這可能是壞的建議,因爲您可能不瞭解完全在全局範圍內運行腳本的安全影響。
  • 因此,(更好的)替代方法是手動foreach ($_REQUEST as $key=>$value),從而填滿你的$ _SESSION。在第二個腳本中同樣這樣做。
    切記:Foreach循環是我們的朋友
  • 同樣,html輸出可以在循環中完成,減少了整體代碼的大小。
  • 您可能會缺少session_start,或者可能是:有過早的輸出,並得到了一個你沒有告訴我們的通知。
3

在讀取或寫入$ _SESSION變量之前,必須調用session_start。在第二頁「錯誤頁面」中,您放置了session_start,但在您嘗試讀取$ _SESSION var之後,請將其放置在「錯誤頁面」的頂部而不是中間。

0

除了已經討論過的東西之外:您需要將會話ID放入您重定向到的URL中,以便在不能使用Cookie的情況下將會話ID存在。

header ('location:referraldone.php?say=blankfields&'.SID); 

這當您使用session.use_trans_sid指定