Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

problems with sessions

Recommended Posts

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]
<?php
session_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">&nbsp;</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>&nbsp;</td>
              <td>&nbsp;</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!

Share this post


Link to post
Share on other sites
you forgot session_start(); in your second file.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
  $_SESSION['contest']=$_POST['dname'];

wheres the second condition set to session comming from.

Share this post


Link to post
Share on other sites
Oh, ignore my post... Lol i think redarrow is right...

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
in the second form... or which ever one youre having trouble with, at the very top of the page try putting
session_start();
echo $_SESSION['contest'];

and see if it echos anything

Share this post


Link to post
Share on other sites
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 putting
session_start();
echo $_SESSION['contest'];

and see if it echos anything
[/quote]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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']; }

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
hmmm try
if(($_POST['dname']) && ($_POST['dname'] != "")) { $_SESSION['contest'] = $_POST['dname']; }
else { //what to do if the post value isnt set. }

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.