Bizarre (I think) session behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Plankmeister

    Bizarre (I think) session behaviour

    Hi...

    I'm using session_set_sav e_handler() and have defined some functions to
    store session data in a database. It all works marvellously. Except for one
    small thing. If I do something like:

    ...
    if(isset($_SESS ION['my_variable']))
    ...

    the system calls the sess_write function, but passes an empty string as the
    session variable parameter. Is this correct? I'm getting around this by
    using an if clause and returning false if it's an empty string. This seems
    to work ok. But is this correct behaviour? Or is it a bug?

    TIA.

    Plankmeister.


  • CountScubula

    #2
    Re: Bizarre (I think) session behaviour

    "The Plankmeister" <plankmeister_N OSPAM_@hotmail. com> wrote in message news:<3fee0949$ 0$27450$edfadb0 f@dread16.news. tele.dk>...[color=blue]
    > Hi...
    >
    > I'm using session_set_sav e_handler() and have defined some functions to
    > store session data in a database. It all works marvellously. Except for one
    > small thing. If I do something like:
    >
    > ...
    > if(isset($_SESS ION['my_variable']))
    > ...
    >
    > the system calls the sess_write function, but passes an empty string as the
    > session variable parameter. Is this correct? I'm getting around this by
    > using an if clause and returning false if it's an empty string. This seems
    > to work ok. But is this correct behaviour? Or is it a bug?
    >
    > TIA.
    >
    > Plankmeister.[/color]


    This is correct behaviouir, you are checking to see if it isset,
    basicaly a variable in use, it can contain nothing, example

    isset($a) : false

    $a = ""; // empty string, basicaly set to nothing, but set it is
    isset($a) // evals to true

    $a = array(); // empty array()
    isset($a) // vals to tru

    $a = ""; // empty string, basicaly set to nothing, but set it is
    unset($a);// removes string from existance, off into nerver, never..
    isset($a) // evals to false


    change your if to this:
    if( isset($_SESSION['my_variable'] != "" )


    Mike Bradley
    http://gzen.myhq.info -- free online php tools

    Comment

    • Agelmar

      #3
      Re: Bizarre (I think) session behaviour

      CountScubula wrote:[color=blue]
      > "The Plankmeister" <plankmeister_N OSPAM_@hotmail. com> wrote in
      > message news:<3fee0949$ 0$27450$edfadb0 f@dread16.news. tele.dk>...[color=green]
      >> Hi...
      >>
      >> I'm using session_set_sav e_handler() and have defined some functions
      >> to
      >> store session data in a database. It all works marvellously. Except
      >> for one
      >> small thing. If I do something like:
      >>
      >> ...
      >> if(isset($_SESS ION['my_variable']))
      >> ...
      >>
      >> the system calls the sess_write function, but passes an empty string
      >> as the
      >> session variable parameter. Is this correct? I'm getting around this
      >> by
      >> using an if clause and returning false if it's an empty string. This
      >> seems
      >> to work ok. But is this correct behaviour? Or is it a bug?
      >>
      >> TIA.
      >>
      >> Plankmeister.[/color]
      >
      >
      > This is correct behaviouir, you are checking to see if it isset,
      > basicaly a variable in use, it can contain nothing, example
      >
      > isset($a) : false
      >
      > $a = ""; // empty string, basicaly set to nothing, but set it is
      > isset($a) // evals to true
      >
      > $a = array(); // empty array()
      > isset($a) // vals to tru
      >
      > $a = ""; // empty string, basicaly set to nothing, but set it is
      > unset($a);// removes string from existance, off into nerver, never..
      > isset($a) // evals to false
      >
      >
      > change your if to this:
      > if( isset($_SESSION['my_variable'] != "" )[/color]

      Generally I would agree with Mike, except that the above code has two
      problems. One, you're testing whether a boolean value is set
      ($_SESSION['something'] == 'something') will always evaluate to true or
      false. This is pointless.

      Secondly, to generalize, you want to know if something is empty, not equal
      to the empty string. Therefore, for both semantic and readability reasons,
      you should use empty().

      empty($somevar) returns true if $somevar is
      a) not defined (e.g. isset($somevar) == false)
      b) equal to ""
      c) equal to 0
      d) equal to "0"
      e) equal to NULL
      f) equal to FALSE
      g) equal to array()
      h) equal to var $var (defined but not set to equal anything)
      i) an object with empty properties.


      Comment

      • CountScubula

        #4
        Re: Bizarre (I think) session behaviour

        > Secondly, to generalize, you want to know if something is empty, not equal[color=blue]
        > to the empty string. Therefore, for both semantic and readability reasons,
        > you should use empty().
        >[/color]

        Ah yes, I stand corrected. :)


        Mike Bradley
        http://gzen.myhq.info -- free online php tools

        Comment

        Working...