Problem with PHP sessions [closed]
Update Solved problem!
After examining the code, the freaking problem was I was missing the }else{}
after
header ('location:referraldone.php?say=blankfields');
}
.
I'm really lost at the moment with this session code. I don't understand why the session isn't working. Where did I go wrong? I'm trying to take the data entered in the fields to the next page when someone makes an error not filling an required field. Codes are below:
<?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 />&l开发者_JAVA百科t;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>
Here's the error page that should take the session data and fill the fields with it, but it doesn't:
<?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>
A few obvious problems with your code:
- Your are using binary OR
|
instead of the boolean or||
in yourif
clause. - Instead of manually localizing the variables, you might as well
extract($_REQUEST);
- This is probably bad advise, since you might not understand the security implications of your script running entirely in the global scope. - So the (better) alternative would be to manually
foreach ($_REQUEST as $key=>$value)
and thus fill up your $_SESSION. Do so likewise in the second script.
Remember: Foreach loops are our friends. - Likewise the html output can be accomplished in a loop, reducing the overall code size.
- You might be missing a session_start, or also likely: have premature output and got a notice which you didn't tell us about.
session_start must be called before you read from -or write to- the $_SESSION variable. In the second page "error page", you placed session_start but after you tried to read the $_SESSION var, so place it in the top of that "error page" instead of the middle.
Okay, some rewriting help.
I would ultimately start by making a descriptive array of the field names and titles. This way you have a secure list of acceptable input fields and simplify the output later on.
$allowed_fields = array("
"cname" => "* Client name", // titles may include <b>HTML</b>
"phone" => "PHone number",
"caddress" => "Address",
...
");
But to simplify the first script, you can replace the 50 lines of $_REQUEST input and $_SESSION filling with just:
session_start();
foreach ($_REQUEST as $field=>$value) {
$_SESSION[$field] = $_REQUEST[$field];
}
And all the form field output can be simplified this way:
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
}
additional to the things that has already been talked about: You'll need to put the session-ID into the URL you redirect to, to have it present if cookies cannot be used.
header ('location:referraldone.php?say=blankfields&'.SID);
This will not be done automatic, even not when you use session.use_trans_sid
精彩评论