2011-01-22 123 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指定