#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171

    Unknown PHP notice


    Hi;

    Why do I get this notice?
    A PHP Error was encountered
    Severity: Notice
    Message: Undefined property: Password_forgot::$logged
    Filename: libraries/Header.php
    Line Number: 14
    PHP Code:
     class Header 
    {
     public 
    $logged '';
     function 
    index()
     {
      
    $CI =& get_instance();
      if(isset(
    $CI->session->userdata['logged_data']['logged_in']))
      {
       
    $CI->logged $CI->session->userdata['logged_data']['logged_in'];
      }
      
      if(
    $CI->logged!=TRUE)
      {
       
    $data['add_deal_link'] = "log_in";
       
    $data['log_in_link'] = base_url('log_in');
       
    $data['login_text'] = 'Log in';
       
    $data['logout'] = '';
      }
      else
      {
       
    $data['add_deal_link'] = "add_deal";
       
    $data['log_in_link'] = base_url('home');
       
    $data['login_text'] = 'My Account';
       
       
    $data['logout'] = '<li><a href="'.base_url('log_out').'">Log Out</a></li>';
      }
      
      
    $CI->load->view('header_view',$data);
     }

  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    I can't be sure because you've only posted a small amount of code, but I bet the Password_forgot class does not have a $logged property like the Header class does.

    By the way,
    PHP Code:
    $CI =& get_instance(); 
    No references. With PHP 5 the "reference" happens automatically for classes.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    $CI->logged will only be set in case the if part is executed. Otherwise, it's not set. Since you try to access it either way, you risk getting a notice -- which is what just happened.

    For a cleaner programming style, make sure $CI->logged is always set. Either define a default value or add an else branch.

    Comments on this post

    • requinix agrees : howtf did I not see that... okay, that's it, I'm done for the day
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    By the way,
    PHP Code:
    $CI =& get_instance(); 
    No references. With PHP 5 the "reference" happens automatically for classes.
    Please see this (scroll down).
    Originally Posted by requinix
    I can't be sure because you've only posted a small amount of code, but I bet the Password_forgot class does not have a $logged property like the Header class does.
    I load header class on almost every page
    PHP Code:
    $this->load->library('header');
    $this->header->index(); 
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    $CI->logged will only be set in case the if part is executed. Otherwise, it's not set. Since you try to access it either way, you risk getting a notice -- which is what just happened.

    For a cleaner programming style, make sure $CI->logged is always set. Either define a default value or add an else branch.
    Sorry but would you please show with example? Thank you
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by English Breakfast Tea
    Sorry but would you please show with example? Thank you
    Bad style:

    PHP Code:
    <?php

    session_start
    ();


    if (isset(
    $_SESSION['user_id']))
    {
        
    $logged_in true;
    }

    if (
    $logged_in)    // This may throw a notice
    {
        ...
    }
    The problem is that $logged_in will be either true or not defined at all. When trying to use it, you may get a notice.



    Better style:

    PHP Code:
    <?php

    session_start
    ();


    $logged_in false;

    if (isset(
    $_SESSION['user_id']))
    {
        
    $logged_in true;
    }

    if (
    $logged_in)
    {
        ...
    }
    The variable will be defined in any case, so it's safe to use it.



    Originally Posted by English Breakfast Tea
    Please see this (scroll down).
    This information is totally outdated. It refers to PHP 4, which died in 2008. Since PHP 5, object variables only contain an ID, so copying them does not copy the whole object.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo