Re: PHP session variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Aug 15, 2012 at 3:24 PM, Tedd Sperling <tedd@xxxxxxxxxxxx> wrote:
> Your points are well taken -- thanks.

I've seen a lot of people code that way, so it's easy to miss. In your
original code, that first statement was calling session_start() 1,000
times. This is because the first time through, SID is undefined so
defined(SID) was equivalent to defined('SID') and both would have
returned false. After the first session_start(), though, SID WAS
defined, but would have had some pseudo-random session identifier as
its value. As a result, the last 999 times through your loop, you were
actually scanning the defined constants to see if there was one named
something like '7cjubadsh5lkq80opemht2ea03'. Obviously, it would never
exist.

> However, my only concern is given this:
>
>>      for($i=1; $i < 1000; $i++)
>>        {
>>        if (!defined('SID'))
>>          {
>>          echo __LINE__, '::session_start()<br>';
>>          session_start();
>>          }
>>        }
>
> The php manual ( http://us3.php.net/manual/en/function.session-start.php )
>
> First Note states that session_start() must be called *before* anything sent to the Browser.
>
> So, to rewrite your code --
>
>      for($i=1; $i < 1000; $i++)
>        {
>        if (!defined('SID'))
>          {
>          session_start();
>          echo __LINE__, '::session_start()<br>';
>          }
>        }
>
> -- should work better, right?
>
> Cheers,
>
> tedd


Yes, that is more correct. I think we have output buffering enabled on
most of our servers (a lot of our legacy stuff really depends on it)
so I didn't notice any errors, but you are correct. You really don't
need the echo lines that I added in your test at all though. I just
threw it in there to be able to see when the function was being
called. However, due to the nature of your test page you are still
sending output from the first loop before you call session_start() in
the second loop. To be absolutely correct, you'd have to remove those
echo statements I added for debugging, store all of your timings in
separate variables and then output them at the end of the script.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux