Accessing $_SESSION variables in PHP5 needs isset() first?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro Fonseca

    Accessing $_SESSION variables in PHP5 needs isset() first?

    Greetings everyone!

    I'm porting everything to PHP5. I have session variables in all of my
    web application. Until PHP5 I was using session variables like:

    if ($_SESSION['foo'] == 'Bar') {
    $value = 5;
    }

    $_SESSION['foo'] is of course set on some other script. But this now
    generates a NOTICE error:

    NOTICE: Undefined index: foo in ....... on line ...

    The only way I can think of to get around this (without, of course,
    turning Notices off in the php.ini) is to first use isset on the
    session variable, like this:

    if ( isset($_SESSION['foo']) && $_SESSION['foo'] == 'Bar' ) {
    $value = 5;
    }

    Is this really necessary? Or is there any other way of doing this in
    PHP5? IMHO the whole point of session variables is that they don't
    really have to be defined in one particular script, so why that
    NOTICE?... Do I really need the superfluous call to isset()?
  • Jochen Daum

    #2
    Re: Accessing $_SESSION variables in PHP5 needs isset() first?

    Hi,

    On 27 Jul 2004 15:24:48 -0700, nospam@pedrofon seca.com (Pedro Fonseca)
    wrote:
    [color=blue]
    >Greetings everyone!
    >
    >I'm porting everything to PHP5. I have session variables in all of my
    >web application. Until PHP5 I was using session variables like:
    >
    >if ($_SESSION['foo'] == 'Bar') {
    > $value = 5;
    >}
    >
    >$_SESSION['foo'] is of course set on some other script. But this now
    >generates a NOTICE error:
    >
    >NOTICE: Undefined index: foo in ....... on line ...
    >[/color]

    I think default error_reporting () has changed. Look up that function.
    [color=blue]
    >The only way I can think of to get around this (without, of course,
    >turning Notices off in the php.ini) is to first use isset on the
    >session variable, like this:
    >
    >if ( isset($_SESSION['foo']) && $_SESSION['foo'] == 'Bar' ) {
    > $value = 5;
    >}
    >
    >Is this really necessary?[/color]

    I personally find error_reporting very helpful, because it tells you
    some of the typos and AFAIK in PHP5 even more of them.

    However, you need to use isset everywhere. I think it is worthwhile.
    [color=blue]
    > Or is there any other way of doing this in
    >PHP5? IMHO the whole point of session variables is that they don't
    >really have to be defined in one particular script, so why that
    >NOTICE?... Do I really need the superfluous call to isset()?[/color]


    HTH, Jochen

    --
    Jochen Daum - Cabletalk Group Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

    Comment

    • Chung Leong

      #3
      Re: Accessing $_SESSION variables in PHP5 needs isset() first?


      "Pedro Fonseca" <nospam@pedrofo nseca.com> wrote in message
      news:15109a72.0 407271424.7eb28 74b@posting.goo gle.com...[color=blue]
      > The only way I can think of to get around this (without, of course,
      > turning Notices off in the php.ini) is to first use isset on the
      > session variable, like this:
      >
      > if ( isset($_SESSION['foo']) && $_SESSION['foo'] == 'Bar' ) {
      > $value = 5;
      > }
      >
      > Is this really necessary? Or is there any other way of doing this in
      > PHP5? IMHO the whole point of session variables is that they don't
      > really have to be defined in one particular script, so why that
      > NOTICE?... Do I really need the superfluous call to isset()?[/color]

      One thing you can do with PHP5 is wrap $_SESSION in an object and access the
      element as properties:

      class DataObject {
      private $values;
      function __construct($va lues, $default = array()) {
      $this->values = array_merge($de fault, $values);
      }
      function __get($name) {
      return isset($this->values[$name]) ? $this->values[$name] : null;
      }
      }

      $Session = new DataObject($_SE SSION);

      if($Session->foo == 'bar') {
      ...
      }

      Property access is one of the neatest features of PHP5. Has the potential of
      majorly decluttering your code. In an expanded version of the class above,
      for example, I have the get function automatically escaping the property
      value based on a suffix. $Post->foo_html would be
      htmlspecialchar s($_POST['foo']), $Post->foo_sql would be
      mysql_escape_st ring($_POST['foo']), etc.

      Unfortunately it seems to be one of the buggier features. My code blew up
      when I had an array in there :-(


      Comment

      Working...