Firefox losing session data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Firefox losing session data

Post by Belkorin »

I am having a very strange issue. I have a script that runs perfectly, flawlessly on all browsers on one server, but on another server, it loses my session data, but not the sessionid or the session cookie, and only in firefox. Also, making the matter more confusing, session data is not lost in other scripts on the same server, from the same web app, and all of the scripts are (theoretically) handling sessions in the same manner.

I'm not sure what information will be relevant to figuring out the solution here, so if anyone thinks they know what's going on and needs additional information, please let me know.

Some basic information: PHP5, Apache2, https website with secure cookies

Thank you
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Firefox losing session data

Post by McInfo »

Let's start by looking at the code in the problem script.

When you post code in the forum, remember to put your code between BBCode tags

Code: Select all

[php][/php]
or

Code: Select all

[code=php]
[/code]
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:34 pm, edited 1 time in total.
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

Here's the parts of the code relevant to sessions.

Code: Select all

    
 
    require_once('irp.php');
    session_start();
 
    if(!isset($_POST['next']) && !isset($_POST['prev']))
    {
        session_destroy();
        session_start();
        echo "destroyed session, starting over<br>";
        $_POST['qnum']=0;
    }
    
    $tpl = array();
    $tpl['base_url'] = pl_settings_get('base_url');
    //echo $tpl['base_url'];
    $tpl['title_image'] = '/images/EligibilityTitle.gif';
    $tpl['title_text'] = 'Check Your Eligibility';
    
    if(verify_answer($_POST['qnum']))
    {
        next_question();
    }
    else
    {
        //...unrelated code removed from here.
        exit();
    }
 
function verify_answers($qnum)
{
 
    //...unrelated code removed from here
 
    if(isset($sp['end']))
    {
        //echo "END!";
        if(pass($reject_if, $type))
        {
            if($type !== 'income')
            {
                $_SESSION[$value_name] = $_POST['answer'];
            }
            elseif($type === 'income')
            {
                $_SESSION[$value_name] += $_POST['answer'];
            }
            //echo "<br><br><b>ACCEPTED!</b><br>";
            finish();           
        }
    }
    //normal
    else
    {
        //check conditions
        if($type === 'sum')
        {
            $_POST['answer'] = $_SESSION[$value_name] + $_POST['answer'];
        }
        if(pass($reject_if, $type))
        {
            if($type !== 'income')
            {
                $_SESSION[$value_name] = $_POST['answer'];
            }
            elseif($type === 'income')
            {
                $_SESSION[$value_name] += $_POST['answer'];
            }
            
            (int)$_POST['qnum'] = isset($sp['next'])?$sp['next']:($_POST['qnum']+1);
            
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
 
function pass($reject_if, $type)
{
        //...unrelated code removed from here
        if($type==='household')
    {
        $adults = $_POST['adults'];
        $children = $_POST['children'];
        $family = $adults + $children;
        $_SESSION['adults'] = $adults;
        $_SESSION['children'] = $children;
        $_POST['answer'] = $family;
        
        $result = pl_query("SELECT `label` FROM `menu_poverty` WHERE `value`=$family");
        if($result->numRows() == 0)
        {
            $result = pl_query("SELECT `label`,`value` FROM `menu_poverty` WHERE `value` IN(0,(SELECT COUNT(*) FROM `menu_poverty`)-1) ORDER BY `value`");
            $row = $result->fetchRow();
            $pov_zero = $row['label'];
            $row = $result->fetchRow();
            $pov_top = $row['label'];
            $difference = $family - $row['value'];
            $_SESSION['poverty'] = ($pov_top + $pov_zero*$difference)*1.25;
        }
        else
        {
            $row = $result->fetchRow();
            $_SESSION['poverty'] = $row['label']*1.25;
        }
        
        return TRUE;
    }
    elseif($type==='income')
    {
        $_SESSION['income_count']++;
        $income_amt = $_POST['income_amt'];
        $income_freq = $_POST['income_freq'];
        $income = 0;
        if($income_freq == 'weekly')
        {
            $income = $income_amt * 52;
        }
        elseif($income_freq == 'bi-weekly')
        {
            $income = $income_amt * 26;
        }
        elseif($income_freq == 'monthly')
        {
            $income = $income_amt * 12;
        }
        $_SESSION['income_amt'.$_SESSION['income_count']]=$income_amt;
        $_SESSION['income_freq'.$_SESSION['income_count']]=$income_freq;
        $_POST['answer'] = $income;        
        
        if(!isset($_SESSION['poverty']))
        {
            die('household size question must come first');
        }
        
        if(($_SESSION['income']+$income) > $_SESSION['poverty'])
        {
            $tpl['main'] = $tpl['main']."fail";
            session_destroy();
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    elseif(sizeof($conds) < 2)
    {
        return TRUE;
    }
    else
    {
        $oper = $conds[0];
        $cond = $conds[1];
        
        //check for variables
        if(substr($cond,0,1)==='$')
        {
            $cond = substr($cond,1);
            if(isset($_SESSION[$cond]))
            {
                $cond = $_SESSION[$cond];
            }
            elseif(isset($sp[$cond]))
            {
                $cond = $sp[$cond];
            }
            else
            {
                die("required variable $".$cond." not set");
            }
        }
        
        $fail = TRUE;
        
                //unrelated condition checking code removed from here
        
        return !$fail;
    }
}
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Firefox losing session data

Post by McInfo »

Are you sure that $_POST['next'] and $_POST['prev'] are set every time you call the script?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:34 pm, edited 1 time in total.
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

$_POST['next'] is always set, because it's the form submit button, and the other two lines in that if block are never called.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Firefox losing session data

Post by Eric! »

So if you do

Code: Select all

echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
after your start session is it only empty with firefox on that server?
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

Yes, but only in this one script.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Firefox losing session data

Post by McInfo »

Is the verify_answer() function called on line 20 supposed to be the same function as verify_answers() defined on line 30?

Also, the verify_answers() function definition is missing a closing curly brace.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:35 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Firefox losing session data

Post by Eric! »

Barring the errors mcinfo found... You say this code works with other browsers, so maybe those errors are cut/paste errors.
Belkorin wrote:Yes, but only in this one script.
Sorry to be anal about this but many people dump their problems in here without really looking at it. So I have to ask: Have you actually stuck in the var_dump lines at line 5 to see what is stored in $_session for firefox vs others?
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

Yes, I have put it there, and the previously mentioned errors are copy-paste errors.

After going through a few questions, Firefox says:
array(0) { }

IE says:

array(3) {
["i_agree"]=>
string(10) "understood"
["about_restricted"]=>
string(2) "No"
["skip3"]=>
bool(true)
}

And I know that it works on another server because it dumps all of the answer data(stored in $_SESSION) to a database when the series of questions is done.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Firefox losing session data

Post by McInfo »

Are you using Javascript or some other client-side scripting language to set or modify the POST variables?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:36 pm, edited 1 time in total.
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

Nope, this project is 100% client-side scripting free.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Firefox losing session data

Post by mattpointblank »

Are you using Firebug on Firefox? I had some problems with it recently - try disabling it and running your script?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Firefox losing session data

Post by McInfo »

I probably should have mentioned this sooner. Check the contents of $_POST at the beginning of the script.

Code: Select all

echo '<pre>', print_r($_POST, 1), '</pre>';
exit;
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:36 pm, edited 1 time in total.
Belkorin
Forum Newbie
Posts: 11
Joined: Fri Jun 26, 2009 9:03 pm

Re: Firefox losing session data

Post by Belkorin »

I am not using firebug. This problem occurs on any computer using firefox.

Also, $_POST has the expected values in it.
Post Reply