Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Controller Execution
#1

[eluser]plxkarl[/eluser]
I'm having the following problem. To simplify it this is what I'm testing:
- sessions are turned off
- compress_output is off

in one of the functions in my controller i put the following code:

Code:
session_start();

print $_SESSION['test'].'<br>';
$_SESSION['test']=time();
print $_SESSION['test'].'<br>';

and i keep refreshing that page. What I expect for every refresh (not the initial refresh) is that the first number printed will equal to the last number printed in last page visit. eg:
Code:
//2nd page visit:
222222222001
222222222020     //this number should be the same as

//3rd page visit:
222222222020     //this one
222222222043

however most of the time it is always few seconds off. when i tested that code without using codeigniter it was correct for every refresh.

It seems as if codeigniter is running that controller function twice. Does anybody know where the problem comes from?

I've also tested that code with standard codeigniter session and it still is few seconds off.
#2

[eluser]plxkarl[/eluser]
Hmmm... this is getting interesting. It seems that it's not the problem is not with code igniter but with how php classes are instantiated. If the controller execution takes more than 1 second it will offset my $_SESSION['test'] variable. Check this out:

Code:
class test{
    function __construct(){
                sleep(5);
    }
    function run(){
        session_start();
        print $_SESSION['test'].'<br>';
        $_SESSION['test']=time();
        print $_SESSION['test'].'<br>';
    }
}

$t=new test();
$t->run();
#3

[eluser]plxkarl[/eluser]
Hmmm... Nevermind it's not the fault of the constructor, now I have no idea what is going on, check this out:

Code:
session_start();
$GLOBALS['mytime']=time();

class test{
    function __construct(){
                sleep(1);
    }
    function run(){        
        print $_SESSION['test'].'<br>';
        $_SESSION['test']=$GLOBALS['mytime'];
        print $_SESSION['test'].'<br>';
    }
}

$t=new test();
sleep(2);
$t->run();

It has something to do with how long the entire page executes. But I dont know what it really is.
#4

[eluser]plxkarl[/eluser]
and here's much simpler example:

Code:
session_start();

print $_SESSION['test'].'<br>';
$_SESSION['test']=time();
print $_SESSION['test'].'<br>';

sleep(3);
print 'DONE<br>';

now why is $_SESSION['test'] offset by 3 seconds?
#5

[eluser]plxkarl[/eluser]
another discovery, this only happens in firefox... tried it in ie8 and chrome and it seems to be working fine. how could it be the problem with the browser if sessions are stored on the server and browser only has session_id in the cookie?
#6

[eluser]plxkarl[/eluser]
And this seems to fix it:
Code:
session_start();

ob_start();

    print $_SESSION['test'].'<br>';
    $_SESSION['test']=time();
    print $_SESSION['test'].'<br>';
    
    sleep(3);
    print 'DONE<br>';

ob_end_flush();
#7

[eluser]plxkarl[/eluser]
Actually the previous solution didn't fix it. There was nothing wrong with that script at all, the problem was with the firefox plugin 'YSlow', it was turned on and for every request to the server I made YSlow was making another one (after my request completed) which in turn was modifying my session variable's time(). PEACE!!!




Theme © iAndrew 2016 - Forum software by © MyBB