Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP script randomly losing session variables?

align session session variable

  • Please log in to reply
3 replies to this topic

#1 gakattack

gakattack

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 10 January 2009 - 08:43 AM

John | Please use
, [code], and [quote] tags where appropriate. [/COLOR][/B]

I have a php script that passes session data to itself (it's a single page that changes through a series of steps.) Anyway, there is an include file that it uses as well.

The problem is that randomly, the session variable will get unset and will go back to the first step because of this. The ONLY section of code where this variable gets unset, is not being executed... Sometimes it will happen 1/7 times, sometimes it will happen 1/21 times. It doesn't depend on the user input at all either.

Thanks for the help! Below is some sample code (the variable in question is the $_SESSION['count'] variable):

[B]HTACCESS:[/B]
[code]php_flag session.use_only_cookies on
php_flag session.use_trans_sid off
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blender/
#RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{REQUEST_FILENAME} !blender_control\.php
RewriteCond %{REQUEST_FILENAME} !blender_redirect\.php
RewriteCond %{REQUEST_FILENAME} !\.gif
RewriteCond %{REQUEST_FILENAME} !\.jpg
RewriteCond %{REQUEST_FILENAME} !\.png
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteCond %{REQUEST_FILENAME} !\.swf
RewriteCond %{REQUEST_FILENAME} !\.inc
RewriteCond %{REQUEST_FILENAME} !\.dat
RewriteCond %{REQUEST_FILENAME} !\.txt
RewriteCond %{REQUEST_FILENAME} !\.js
RewriteRule . /blender/blender_control.php [L]
</IfModule>[/code]

[B]The Include File[/B][php]
<?php session_start();
.
.
.
function showQuestion($i) {
global $numquestions, $q, $a, $mchoice;
echo "<form method=POST ><br>";
echo "<input type=hidden name=next value=$i >";
echo "<input type=hidden id=idSelection name=selection value=-1 >";
echo "<table cellpadding=1 ><th halign=left colspan=2 > <h2 align=center>$q[$i]</h2> <br></th>";
$mylist = split (";", $mchoice[$i]);
$j=0;
foreach ($mylist as $keys => $values) {
$id="rdo".$j;
echo "<tr ><td align=right valign=center size=40% > <input type=radio id=rdo0 name=$i value='$values' onClick='onClickRadio($j)'></td>";
echo "<td align=left valign=center size=60% ><h1> $values </h1></td></tr>";
$j++;
}

echo "<tr><td colspan=2 align=center ><br><input type=image src=images/submit.gif name=Continue value=Continue onClick='onClickContinue()' \> </td></tr></table>";
};
.
.
.
?>[/php]

[B]The main script[/B][php]
<?php session_start();
.
.
.
if (isset ($_SESSION['count'])) {
$_SESSION['count'] = $_SESSION['count'] + 1;
}

else {
$_SESSION['count']=1;
$_SESSION['score']=0;
$_SESSION['maxscore'] =0;
};
.
.
.
#--- check if user submitted form --#
elseif (isset($_POST["next"])) {


$_SESSION['score'] += calculateScore($_SESSION['count'] - 1);

//numquestions is a constant by the way..it doesn't change..
if ($numquestions >= $_SESSION['count'] ) {

if($numquestions > $_SESSION['count']) {
echo "<h3>Question " . $_SESSION['count'] . " of 10</h3 >\n";
}
showQuestion($_SESSION['count']);
}
}
else { #show first question
echo "<h3>Question 1 of 10</h3 >";
showQuestion(1);
$_SESSION['count'] = 1;
}
.
.
.
elseif (isset ($_POST['var'])) {
//not being executed. it would make sense if it was...
echo "<script type=\"text/javascript\"> checkPop(); </script>";

include ("blah.php");

#---- reset session vars ---#
unset ($_SESSION['count']);
unset ($_SESSION['max']);
unset ($_SESSION['score']);

}

[B]John | [COLOR="Red"]Please use [php], [code=auto:0], and [quote] tags where appropriate.

Edited by John, 10 January 2009 - 12:09 PM.

  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 10 January 2009 - 02:41 PM

I believe there is something with this line:

elseif (isset ($_POST['var'])


I feel it safer with post & get variables to check if they are !='' instead of if they are set. I don't exactly why, but I know that these variables can make things go nuts at times, if you use them without respect for falsifications. The best way in my opinion is to always set this var to a specific value in a hidden input, and set it to something else at the time you want this to be run.
  • 0

#3 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 10 January 2009 - 03:24 PM

The first thing that jumped to my mind is your session is expiring. Can you verify they get unset at different times and not just at different places?
  • 0

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 11 January 2009 - 02:52 AM

I must agree with John at closer thought.
  • 0





Also tagged with one or more of these keywords: align, session, session variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download