Jump to content

Troubleshooting a bug with $_SESSION...


sepoto

Recommended Posts

 <?phpsession_start(); $p_email=$_SESSION["email_address"];$p_last=$_SESSION["last_name"];$p_first=$_SESSION["first_name"]; $dbh = new PDO('mysql:host=financial-lemonade.com;dbname=finlem', 'root', 'Feeds777');$count = $dbh->exec("set sql_safe_updates=0;"); $res=$dbh->query("select * from users where email=\"$p_email\";");$count = $res->rowCount();if($count == 0){$dbh->exec("insert into users(first,last,email) values(\"$p_first\",\"$p_last\",\"$p_email\");");}session_write_close();session_destroy(); header("location: http://www.financial-lemonade.com/");?>

For some reason the first time this script is ran I get an empty row in my database even though I am positive I entered data into my form. If I run the program again I get good data entered into the database. It seems there is something wrong with $_SESSION. My PHP version is 5.3.

Link to comment
Share on other sites

How does the data get into $_SESSION in the first place? Where's the form code? That seems like an important part of this.

Edited by thescientist
Link to comment
Share on other sites

 <!DOCTYPE html><html><head><meta charset="utf8"><title>Financial Lemonade - Financial Help Guide</title><meta name="description" content="Financial Hardship resources and information to help struggling americans improve their financial status."/><meta name="keywords" content="Financial Hardship Resources, Financial Hardship Guide, Financial Hardship Assistance Aid Help, Hardship, Financial, Grants, Aid"/><meta name="robots" content="ALL"/><meta name="robots" content="NOYDIR"/><meta name="robots" content="NOODP"/><meta name="author" content="Financial Lemonade - Financial Help Guide"/><meta name="Copyright" content="Copyright (c) 2010 Financial-Lemonade.com"/> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script><style type="text/css">@import url("site.css");  .nav_bar {position: absolute;top: 134px;left: 250px;width: 942px;}.main_bg {background-color: #FFEA55;height: 416px;width: 900px;border: 2px solid #F90;position: absolute;left: 270px;top: 173px;-moz-border-radius: 15px;-webkit-border-radius: 15px;-khtml-border-radius: 15px;border-radius: 15px;}.left bar {background-color: #6F9;position: absolute;height: 500px;width: 500px;left: 150px;top: 300px; }.left div span {vertical-align: text-top;}.main_bg1 {background-color: #FFEA55;height: 131px;width: 279px;border: 2px solid #F90;position: absolute;left: 272px;top: 625px;-moz-border-radius: 15px;-webkit-border-radius: 15px;-khtml-border-radius: 15px;border-radius: 15px;}.main_bg11 {background-color: #FFEA55;height: 131px;width: 279px;border: 2px solid #F90;position: absolute;left: 891px;top: 625px;-moz-border-radius: 15px;-webkit-border-radius: 15px;-khtml-border-radius: 15px;border-radius: 15px;}.main_bg12 {background-color: #FFEA55;height: 131px;width: 279px;border: 2px solid #F90;position: absolute;left: 581px;top: 625px;-moz-border-radius: 15px;-webkit-border-radius: 15px;-khtml-border-radius: 15px;border-radius: 15px;}.main_bg13 {background-color: #FFEA55;height: 40px;width: 900px;border: 2px solid #F90;position: absolute;left: 273px;top: 790px;-moz-border-radius: 15px;-webkit-border-radius: 15px;-khtml-border-radius: 15px;border-radius: 15px;}.nav_bar2 {position: absolute;top: 854px;left: 250px;width: 942px;text-align: center;background-color: none;height: auto;margin-top: 8px;margin-bottom: 10px;font-size: 24px;font-weight: bold;color: #063;padding: 2px;list-style-position: outside;border-top-width: 2px;border-bottom-width: 2px;border-top-style: solid;border-bottom-style: solid;border-top-color: #030;border-bottom-color: #030;}</style></head><body> <div class="site-tip"><img src="images/lemon.jpg"width="15" height="14" alt="Lemon" title="Financial Financial Help - Resources" /> | <span style="font-weight:bold">Financial Resources are Getting Better Everyday.</span> </div> <div style="position: absolute; left: 12.5%; top: 40px; height: 90px; width: 289px;">        <a href="/" title="Financial Guide Book"><img src="images/financial_lemonade_logo.png" width="280" height="82" alt="Financial Lemonade Logo" title="Financial Help" /></a></div> <div class="top_right_bar"></div><div class="nav_bar">                         </div>  <div class="main_bg">  <div style="background-color: #0000; background-color: rgba(255,255,205,0.5); position: absolute; height: 200px; width: 373px; left: 13px; top: 185px; moz-border-radius: 15px; -webkit-border-radius: 15px; -khtml-border-radius: 15px; border-radius: 15px;"><span style="position:absolute; top: 12px;color:#1A1A1A;font-size:22px;text-align:center;">   <u><b>Get The Following Resources Now!</b></u></span><span style="position:absolute; top: 25px;color:#1A1A1A;font-size:22px;text-align:left; ">    <ul>      <li>Emergency Cash in 24 Hours</li>      <li>Credit Cards to Pay Bills</li>      <li>Credit Repair Assistance</li>      <li>Money Saving Alternatives</li>      <li>Housing Resources</li>    </ul></span>  </div>  <div style="background-color: #0000; background-color: rgba(255,255,205,0.5); position: absolute; height: 368px; width: 276px; left: 614px; top: 18px; moz-border-radius: 15px; -webkit-border-radius: 15px; -khtml-border-radius: 15px; border-radius: 15px;"><span style="position:absolute; top: 30px; left:25px;">  <!-- constantcontact.form --><form id="ccsfg" name="ccsfg" method="post" action="/CCSFG/signup/index.php"> <h4>Registration Form</h4><p>To join our mailing list, please complete the information below and click 'Join My Mailing List'.</p> <!-- ########## Email Address ########## --><label for="EmailAddress">Email Address</label><input type="text" name="EmailAddress" value="" id="EmailAddress" /><br /> <!-- ########## First Name ########## --><label for="FirstName">First Name</label><input type="text" name="FirstName" value="" id="FirstName" /><br /> <!-- ########## Last Name ########## --><label for="LastName">Last Name</label><input type="text" name="LastName" value="" id="LastName" /><br /> <!-- ########## Contact Lists ########## --><input type="hidden"  checked="checked"  value="General Interest" name="Lists[]" id="list_General Interest" /><!-- ########## Success / Failure Redirects ########## --><input type="hidden" name="SuccessURL" value="http://www.financial-lemonade.com/success.php" /><input type="hidden" name="FailureURL" value="http://www.financial-lemonade.com/error.php" />  <input type="submit" name="signup" id="signup" value="Join My Mailing List" /> </form>               </span>    <span style="position: absolute; bottom: 4px; color: #666; font-size: 12px;text-align: left; padding: 10px; width: 248px; left: 5px;">  By submitting, I certify that I am a US Resident over the age of 18, and I agree to the terms and conditions and privacy policy below.</span >  </div>  </div><div class="left"><div style="background-image: url(images/big_orange_arrow.png); background-repeat: no-repeat; position: absolute; height: 152px; width: 635px; left: 283px; top: 196px; moz-border-radius: 15px; -webkit-border-radius: 15px; -khtml-border-radius: 15px; border-radius: 15px;"><span style="position:absolute; top: 20px;color:#FFF;text-shadow: 2px 1px 3px #333333;font-size:22pt;font-weight:bold;text-align:left;">  Resources to Help Your Financial Situation</span><span style="position:absolute; top: 70px;color:#FFF;text-shadow: 2px 1px 3px #333333;font-size:20pt;font-weight:bold;text-align:left;">  Are You Experiencing Financial Hardship? <br />  Get Help Today!</span></div></div>  <div class="main_bg1"><span style="position:absolute;top:5px;left:30px;color:#640;font-size:22px;text-align:left;"><b>$787 Billion Stimulus!</b><span style="position: absolute; top: 5px; left: -17px; color: #640; font-size: 22px; text-align: left; width: 251px; height: 103px;"><br />It is up to you to claim your piece of stimulus funding.</span></span></div> <div class="main_bg11"><span style="position:absolute;top:5px;left:15px;color:#640;font-size:22px;text-align:center;"><b>Federal Financial Aid!</b><span style="position: absolute; top: 6px; left: -2px; color: #640; font-size: 22px; text-align: left; width: 251px; height: 103px;"><br />  Would you go back to School if you Qualify?</span></span></div><div class="main_bg12"><span style="position:absolute;top:5px;left:25px;color:#640;font-size:22px;text-align:center;"><b>Struggling Financially?</b><span style="position: absolute; top: 5px; left: -17px; color: #640; font-size: 22px; text-align: left; width: 251px; height: 103px;"><br />  Hard to find resources can help you get answers.</span></span></div><div class="main_bg13"><span style="position:absolute;top:10px;left:300px;color:#640;font-size:16px;text-align:center;"><b>© Financial-Lemonade.com • <a href="/privacy_policy"title="Privacy Policy">Privacy Policy</a></b></span></div> <div class="nav_bar2">                     </div>   <!--<script type="text/javascript">$('#ccsfg').submit(function() { $.ajax({ url: 'CCSFG/signup/index.php', data: { EmailAdress: $("#EmailAddress").val(),FirstName: $("#FirstName").val(),LastName: $("#LastName").val()}, type: 'post', success: function (output) {alert(output);}}); return false;});</script>--></body></html>

<?phprequire_once('../setup/functions.php');require_once('../setup/build_functions.php');include_once('../cc_class.php');$ccConfigOBJ = new CC_Config();$ccContactOBJ = new CC_Contact();$ccListOBJ = new CC_List();  if (!empty($_REQUEST)) {  $postFields = array(); // ## PROCESS BASIC FIELDS ## //$postFields["email_address"] = $_REQUEST["EmailAddress"];$postFields["first_name"] = $_REQUEST["FirstName"];$postFields["last_name"] = $_REQUEST["LastName"];$postFields["middle_name"] = $_REQUEST["MiddleName"];$postFields["company_name"] = $_REQUEST["CompanyName"];$postFields["job_title"]= $_REQUEST["JobTitle"];$postFields["home_number"] = $_REQUEST["HomePhone"];$postFields["work_number"] = $_REQUEST["WorkPhone"];$postFields["address_line_1"] = $_REQUEST["Addr1"];$postFields["address_line_2"] = $_REQUEST["Addr2"];$postFields["address_line_3"] = $_REQUEST["Addr3"];$postFields["city_name"] = $_REQUEST["City"];$postFields["state_code"] = $_REQUEST["StateCode"];$postFields["state_name"] = $_REQUEST["StateName"];$postFields["country_code"] = $_REQUEST["CountryCode"];$postFields["zip_code"] = $_REQUEST["PostalCode"];$postFields["sub_zip_code"] = $_REQUEST["SubPostalCode"];$postFields["notes"] = $_REQUEST["Note"];$postFields["mail_type"] = $_REQUEST["EmailType"]; $postFields["success_url"] = $_REQUEST["SuccessURL"];$postFields["failure_url"] = $_REQUEST["FailureURL"]; $postFields["request_type"] = $_REQUEST["RequestType"]; session_start();session_regenerate_id(); $email_address = $postFields["email_address"];$_SESSION["pemail"] = $email_address;$_SESSION["plast"] = $postFields["last_name"];$_SESSION["pfirst"] = $postFields["first_name"];     $p_email=$_SESSION["pemail"];$p_last=$_SESSION["plast"];$p_first=$_SESSION["pfirst"]; $dbh = new PDO('mysql:host=financial-lemonade.com;dbname=finlem', 'root', 'Feeds777');$count = $dbh->exec("set sql_safe_updates=0;");$dbh->exec("delete from users where first=\"\" and last=\"\" and email=\"\";"); $res=$dbh->query("select * from users where email=\"$p_email\";");$count = $res->rowCount();if($count == 0){$dbh->exec("insert into users(first,last,email) values(\"$p_first\",\"$p_last\",\"$p_email\");");$dbh->exec("delete from users where first=\"\" and last=\"\" and email=\"\";");}  session_write_close();session_destroy();   // ## PROCESS CUSTOM FIELDS ## //$postFields["custom_fields"] = array();foreach($_REQUEST as $key=>$val) { if (strncmp($key, 'CustomField', strlen('CustomField')) === 0) {$postFields["custom_fields"][substr($key, strlen('CustomField'), strlen($key)-1)] = $val;} } // ## PROCESS LISTS ## //$allLists = $ccListOBJ->getLists('', true); foreach ($allLists as $k=>$item) {if($_REQUEST['Lists'] && !empty($_REQUEST['Lists'])){if (in_array($item['title'],$_REQUEST['Lists'])) {$postFields["lists"][] = $item['id'];}}else {if (in_array($item['title'],$ccConfigOBJ->contact_lists)) {$postFields["lists"][] = $item['id'];}}}  $contactXML = $ccContactOBJ->createContactXML(null,$postFields); $return_code = $ccContactOBJ->addSubscriber($contactXML); if($postFields['request_type'] == 'ajax'){ $postFields["success_url"]=''; $postFields["failure_url"]=''; } if ($return_code==201) {$error = false;if($postFields["success_url"]){ header('Location:'.$postFields["success_url"]); }else { echo '<div id="code" title="201"></div><h3>Thank you!</h3><p>Your interest preferences have been successfully recorded.</p>'; }} else if ($return_code==409) {$error = true;if($postFields["failure_url"]){ //header('Location:'.$postFields["failure_url"]);header("location: http://www.financial-lemonade.com/error.subscribed.php");}else { echo '<div id="code" title="409"></div><h3>We\'re Sorry!</h3><p>It appears that you are already a subscriber of our mailing list.</p>'; }} else {$error = true;if($postFields["failure_url"]){ header('Location:'.$postFields["failure_url"]); }else { echo '<div id="code" title="'.$return_code.'"></div><h3>We\'re Sorry!</h3><p>It appears that you were not added to our mailing list. This may be due to one or more of the following reasons:<ol><li>You have misspelled your email address</li><li>You did not choose a list to subscribe to</li><li>The system may be busy. Please try again later</li></ol></p>'; }}    }  ?>

Link to comment
Share on other sites

Why are you using session_regenerate_id and session_destroy, what are you trying to do with that? The main thing I see is that your session variables are not named the same, you're looking for different names than you're setting.

Link to comment
Share on other sites

I was able to work through this in time. My problem now seems to be there is a blank row being inserted into MySQL with only an ID but no data for `first`, `last` or email. As far as the calls to session_regenerate_id and session_destroy I was trying to go by the book as much as possible. Perhaps my use of those could use a little bit of work...

Link to comment
Share on other sites

As long as you understand exactly what those functions are doing and you're sure that you need that functionality, then it's fine. If it's inserting blank data it sounds like either the insert code is running before a form was submitted, or the variables that hold the data are empty (possibly because you're using the wrong names).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...