digitalgod Posted July 7, 2006 Share Posted July 7, 2006 hey guys,I seem to be having problems with my sessions, for some reason they're not being stored for very long... so here's what i got[code]<?phpsession_start();//....case "editcont": $process=$_POST['process']; if ($process == "yes") { $process_b=$_POST['process_b']; $_SESSION['contest']=$_POST['dname']; if ($process_b == "yes") { $new_name=$_POST['contest']; $new_description=$_POST['description']; $month=$_POST['month']; $day=$_POST['day']; $year=$_POST['year']; $new_date=$month.'-'.$day.'-'.$year; $new_question=$_POST['question']; $new_answer=$_POST['answer']; print $_SESSION['contest']; //outputs nothing mysql_query("UPDATE ".$prefix."contests SET name='$new_name',description='$new_description',end_date='$new_date',question='$new_question',answer='$new_answer' WHERE name='".$_SESSION['contest']."'") or die(query_error()); $tmpl->add_template("editcont_success"); } else { $result=mysql_query("SELECT * FROM ".$prefix."contests WHERE name='".$_SESSION['contest']."'") or die(query_error()); //session works perfectly here $row=mysql_fetch_array($result); if ($row['id'] != "") { $tmpl->add_template("editcont_form2"); } else { $tmpl->add_template("username_no"); } } } else { $tmpl->add_template("editcont_form1"); } break;?>[/code]everything works perfectly until the 2nd form is submitted for some reason the session gets destroyed.... I really don't have a clue why...here's the 2nd form.[code]<?php$result=mysql_query("SELECT * FROM ".$prefix."contests WHERE name='". $_SESSION['contest'] ."'") or die(query_error());$row=mysql_fetch_array($result);$path = "img/contests/" . $_SESSION['contest'] ."/";$dir = $path . "thumbnails/";$aNames = array();if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file_names = readdir($dh)) !== false) { if ($file_names != "." && $file_names != "..") { array_push($aNames,$file_names); } } closedir($dh); }}$numFiles = count($aNames);if ($numFiles == 0) { $file_names = "no_thumb.jpg"; array_push($aNames,$file_names);}?><script type="text/javascript">function writeMonthOptions(){ var theMonth; var monthCounter; var numMonth; var theDate = new Date(); var month=new Array(12) month[0]="January" month[1]="February" month[2]="March" month[3]="April" month[4]="May" month[5]="June" month[6]="July" month[7]="August" month[8]="September" month[9]="October" month[10]="November" month[11]="December" for (monthCounter = 0; monthCounter < 12; monthCounter++) { numMonth = monthCounter + 1; theMonth = month[monthCounter]; if (numMonth < 10 ) { numMonth = '0' + numMonth; } document.write('<OPTION value=' + numMonth + '>' + theMonth); }}function writeDay(){ var dayCounter; var numDay; //numDay = dayCounter; for (dayCounter = 1; dayCounter <= 31; dayCounter++) { if (dayCounter < 10) { numDay = '0' + dayCounter; } else { numDay = dayCounter; } document.write('<OPTION value=' + numDay + '>' + dayCounter); }}</script><div id="article"><form action="admin.php?a=editcont" method="post" enctype="multipart/form-data" name="form" id="form"><input type="hidden" name="process" value="yes" /><input type="hidden" name="process_b" value="yes" /><input type="hidden" name="size_limit" value="500" /> <input name="size_limit" type="hidden" id="size_limit" value="500" /> <table width="603" border="0"> <tr> <td colspan="2"> </td> </tr> <tr> <td width="151">Contest:</td> <td width="442"><input name="contest" type="text" id="contest" value="<?php echo $row['name']; ?>"/></td> </tr> <tr> <td>Description:</td> <td><textarea name="description" cols="50" rows="10" id="description"><?php echo $row['description']; ?></textarea></td> </tr> <tr> <td>End Date:</td> <td><select name="month" id="month"> <option>Month</option> <script type="text/javascript">writeMonthOptions();</script> </select> <select name="day" id="day"> <option>Day</option> <script type="text/javascript">writeDay();</script> </select> <select name="year" id="year"> <option>Year</option> <? $now = date('y'); for ($i=1; $i<=6; $now++) { if(strlen($now) ==1 ) { $now = '0'.$now; } echo '<option value= "'.$now.'">20'.$now.'</option>'; $i++; } ?> </select></td> </tr> <tr> <td>Images:</td> <td><? echo '<table border="0"> <table width="1%" border="0"> <tr>'; for ($i =0; $i <= $numFiles--; $i++) { echo '<td> <a href="' . $path . ''. $aNames[$i].'" target="_blank"> <img src="' . $dir . '' . $aNames[$i] . '" alt="' . $aNames[$i] .'" /></a> </td>';} echo '</tr> </table>';?> <table width="1%" border="0"> <tr> <td> </td> <td> </td> </tr> </table></td> </tr> <tr> <td>Add Image :</td> <td><input name="file1" type="file" id="file1" /></td> </tr> <tr> <td>Security Question :</td> <td><input name="question" type="text" id="question" value="<?php echo $row['question']; ?>"></td> </tr> <tr> <td>Security Answer :</td> <td><input name="answer" type="text" id="answer" value="<?php echo $row['answer']; ?>" /></td> </tr> </table><input type="submit" value="Edit <?php echo $row['name']; ?>"/><label><input type="reset" name="Reset" value="Reset" /></label></form></div>[/code]any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/ Share on other sites More sharing options...
corbin Posted July 7, 2006 Share Posted July 7, 2006 you forgot session_start(); in your second file. Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54582 Share on other sites More sharing options...
digitalgod Posted July 7, 2006 Author Share Posted July 7, 2006 that didn't change anything.... $_SESSION['contest'] still becomes empty Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54584 Share on other sites More sharing options...
corbin Posted July 7, 2006 Share Posted July 7, 2006 variables with [''] in mysql queries I always try to avoid... Try something like $result=mysql_query("SELECT * FROM ".$prefix."contests WHERE name='contest'") or die(query_error());oh and if all else fails try making it something like $contest = $_SESSION['contest'];$q = ("SELECT * FROM ".$prefix."contests WHERE name='contest'");echo $q;$result = mysql_query($q);and see what it says $q is. Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54586 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 $_SESSION['contest']=$_POST['dname'];wheres the second condition set to session comming from. Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54587 Share on other sites More sharing options...
corbin Posted July 7, 2006 Share Posted July 7, 2006 Oh, ignore my post... Lol i think redarrow is right... Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54589 Share on other sites More sharing options...
digitalgod Posted July 7, 2006 Author Share Posted July 7, 2006 the second condition comes from the first form[code]<div id="article"><form action="admin.php?a=editcont" method="post"><input type="hidden" name="process" value="yes" /><table border="0"><tr> <td>Contest:</td> <td><select name="dname" id="dname"> <option>Choose one</option> <?php$sql='SELECT * FROM '.$prefix.'contests';$req=mysql_query($sql) or die(query_error());while($data = mysql_fetch_assoc($req)) { echo "<option value='" . stripslashes($data['name']) . "'>" .$data['name'] . "</option>";}?> </select></td></tr></table><br /><input type="submit" value="Edit Contest" /></form></div>[/code]and that works perfectly so let's say you choose Contest 01 if I echo $_SESSION['contest'] it will output Contest 01 but as soon as the second form is submitted that session outputs nothing.... Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54591 Share on other sites More sharing options...
corbin Posted July 7, 2006 Share Posted July 7, 2006 in the second form... or which ever one youre having trouble with, at the very top of the page try puttingsession_start();echo $_SESSION['contest'];and see if it echos anything Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54593 Share on other sites More sharing options...
digitalgod Posted July 7, 2006 Author Share Posted July 7, 2006 yup it works perfectly it outputs 'test3'but it's when I submit that form that the session gets destroyed.[quote author=corbin link=topic=99831.msg393342#msg393342 date=1152313384]in the second form... or which ever one youre having trouble with, at the very top of the page try puttingsession_start();echo $_SESSION['contest'];and see if it echos anything[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54595 Share on other sites More sharing options...
corbin Posted July 7, 2006 Share Posted July 7, 2006 In your first form try changing $_SESSION['contest']=$_POST['dname'];to $_SESSION['contest'] = "<name of a table you know exists>"; and see if the scripts after it work then. Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54596 Share on other sites More sharing options...
digitalgod Posted July 7, 2006 Author Share Posted July 7, 2006 aha that worked even after the 2nd form got submitted!so what do I have to do to make it work now? I really need to get the name of the contest from a dropdown and store it in a session Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54599 Share on other sites More sharing options...
digitalgod Posted July 8, 2006 Author Share Posted July 8, 2006 nevermind I just added[code]if(isset($_SESSION['contest'])) { } else { $_SESSION['contest'] = $_POST['dname']; }[/code]and added an unset after the form gets submitted so it's working perfectly now.Just out of curiosity is there a better way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54625 Share on other sites More sharing options...
corbin Posted July 8, 2006 Share Posted July 8, 2006 I wrote my first script that involved sessions 2 days ago, so I'm far from a session expert, but you could just do if(!$_SESSION['contest']) { $_SESSION['contest'] = $_POST['dname']; } Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54628 Share on other sites More sharing options...
digitalgod Posted July 8, 2006 Author Share Posted July 8, 2006 actually my solution isn't good, if a user selects a contest but doesn't submit the 2nd form, when he chooses another contest it will still keep the name of the first contest stored in the session.... Does anyone know a workaround for this? Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54637 Share on other sites More sharing options...
corbin Posted July 8, 2006 Share Posted July 8, 2006 hmmm tryif(($_POST['dname']) && ($_POST['dname'] != "")) { $_SESSION['contest'] = $_POST['dname']; }else { //what to do if the post value isnt set. } Quote Link to comment https://forums.phpfreaks.com/topic/13988-problems-with-sessions/#findComment-54638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.