Jump to content


Photo

problems with sessions


  • Please log in to reply
14 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 10:35 PM

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

<?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;
?>

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.

<?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>

any help would be greatly appreciated!

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 July 2006 - 10:40 PM

you forgot session_start(); in your second file.
Why doesn't anyone ever say hi, hey, or whad up world?

#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 10:45 PM

that didn't change anything.... $_SESSION['contest'] still becomes empty

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 July 2006 - 10:50 PM

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.
Why doesn't anyone ever say hi, hey, or whad up world?

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 10:51 PM

  $_SESSION['contest']=$_POST['dname'];

wheres the second condition set to session comming from.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 July 2006 - 10:53 PM

Oh, ignore my post... Lol i think redarrow is right...
Why doesn't anyone ever say hi, hey, or whad up world?

#7 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 10:56 PM

the second condition comes from the first form

<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>

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....

#8 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 July 2006 - 11:03 PM

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
Why doesn't anyone ever say hi, hey, or whad up world?

#9 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 11:05 PM

yup it works perfectly it outputs 'test3'

but it's when I submit that form that the session gets destroyed.

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



#10 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 July 2006 - 11:10 PM

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.
Why doesn't anyone ever say hi, hey, or whad up world?

#11 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 11:17 PM

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

#12 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 08 July 2006 - 12:19 AM

nevermind I just added
if(isset($_SESSION['contest'])) {
		}
		else {
		$_SESSION['contest'] = $_POST['dname'];
		}

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?

#13 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 08 July 2006 - 12:28 AM

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']; }
Why doesn't anyone ever say hi, hey, or whad up world?

#14 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 08 July 2006 - 01:10 AM

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?

#15 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 08 July 2006 - 01:18 AM

hmmm try
if(($_POST['dname']) && ($_POST['dname'] != "")) { $_SESSION['contest'] = $_POST['dname']; }
else { //what to do if the post value isnt set. }
Why doesn't anyone ever say hi, hey, or whad up world?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users