Re: PHP session variables

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

 



On Fri, Aug 10, 2012 at 11:56 AM, Tedd Sperling <tedd@xxxxxxxxxxxx> wrote:
> On Aug 10, 2012, at 11:45 AM, Tedd Sperling <tedd@xxxxxxxxxxxx> wrote:
>
>> On Aug 9, 2012, at 5:16 PM, Jim Lucas <lists@xxxxxxxxx> wrote:
>>> You are relying on PHP's loose typing.  This is a poor check.
>>>
>>> session_id() returns a string, not boolean.
>>>
>>> You should do this instead.
>>>
>>> if ( session_id() === '' )
>>> ....
>>>
>>>
>>> --
>>> Jim Lucas
>>
>> Thanks Jim -- you're right.
>>
>> What about?
>>
>> if (!defined(SID))
>>       {
>>       session_start();
>>       }
>
> Before you answer, the "(!defined(SID))" is over 50 times slower than "( session_id() === '' )"
>
> Your way is better.
>
> Cheers,
>
> tedd

tedd,

I think this is because you passed SID to defined() as a constant
rather than a string, so the if test always returns true. When I
changed this to

if (!defined('SID'))
    {
    session _start();
    }

it worked as expected, and I got much more similar results. I also
added a call to session_destroy() between the two tests so that each
loop initialized the session once. (In your test, the second loop
never initializes the session since it was already started by the
first loop.)

This is your code with my modifications:

<?php

      $starttime = microtime(true);


      // whatever you want timed, you do here.



      for($i=1; $i < 1000; $i++)
        {
        if (!defined('SID'))
          {
          echo __LINE__, '::session_start()<br>';
          session_start();
          }
        }

      session_destroy();

      $endtime = microtime(true);
      $totaltime = $endtime - $starttime;
      $totaltime = round($totaltime,5);
      echo "<p>First in $totaltime seconds.</p>";

      $starttime = microtime(true);


      // whatever you want timed, you do here.

      for($i=1; $i < 1000; $i++)
        {
        if (session_id() ==='')
          {
          echo __LINE__, '::session_start()<br>';
          session_start();
          }
        }

      $endtime = microtime(true);
      $totaltime = $endtime - $starttime;
      $totaltime = round($totaltime,5);
      echo "<p>Second in $totaltime seconds.</p>";

      session_destroy();

?>

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