PHP会话问题

时间:2011-01-22 01:48:55

标签: php session

更新解决了问题!

在检查代码之后,一个令人讨厌的问题是我在

之后错过了}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>

4 个答案:

答案 0 :(得分:3)

您的代码存在一些明显的问题:

  • 您使用的是二进制OR |,而不是||子句中的布尔值或if
  • 您也可以extract($_REQUEST); - 而不是手动本地化变量 - 这可能是不好的建议,因为您可能无法理解完全在全局范围内运行的脚本的安全隐患。
  • 所以(更好的)替代方案是手动foreach ($_REQUEST as $key=>$value),从而填满你的$ _SESSION。在第二个脚本中同样这样做 请记住: Foreach循环是我们的朋友
  • 同样,html输出可以在循环中完成,从而减少整体代码大小。
  • 您可能错过了session_start,或者也可能:过早输出并收到您没有告诉我们的通知。

答案 1 :(得分:3)

在从-or写入$ _SESSION变量之前,必须先调用session_start。在第二页“错误页面”中,您放置了session_start但在尝试读取$ _SESSION var之后,将其放在“错误页面”的顶部而不是中间。

答案 2 :(得分:1)

好的,有些改写帮助。

我最终会先创建一个字段名称和标题的描述性数组。这样,您就可以获得可接受输入字段的安全列表,并在以后简化输出。

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

但是为了简化第一个脚本,您可以用以下内容替换50行$ _REQUEST输入和$ _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 :(得分:0)

除了已经讨论过的内容之外:您需要将会话ID放入您重定向到的URL中,以便在无法使用Cookie时将其存在。

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

即使使用 session.use_trans_sid

,也不会自动执行此操作