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

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

    Notice: Undefined index


    Why notice?
    Notice: Undefined index: email
    PHP Code:
    $email $_POST['email'] ? $_POST['email'] : 'Email Address'
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Because you're trying to use it without making sure it's defined. The proper format for the ternary check like this is:
    PHP Code:
     $email = isset( $_POST['email'] ) ? $_POST['email'] : 'Email Address'
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by ManiacDan
    Because you're trying to use it without making sure it's defined.
    How about this?
    PHP Code:
    if($_SESSION['logged']==true)
        {
            
    header('Location: profile.php');    
        } 
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    • PHP will generate a notice if the index is not defined
    • There's no exit; after the header call
    • The comparison makes no sense. A boolean already is either true or false. It doesn't get "truer" or "falser".

    The correct way of doing this would be

    PHP Code:
    if(isset($_SESSION['logged']) && $_SESSION['logged'])
        {
            
    header('Location: profile.php');
            exit;    
        } 
    Note that the Location header actually requires an absolute URL. However, since almost nobody gets that right, common browsers will swallow relative URLs as well.
    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".
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    PHP will generate a notice if the index is not defined
    I realise that. How can I stop that in this situation?
    Originally Posted by Jacques1
    The comparison makes no sense. A boolean already is either true or false. It doesn't get "truer" or "falser".
    I think I got this from Northie, but I am not sure if it was him or E-oreo
    PHP Code:
    if(/*username and password match*/)
        {
             
    $_SESSION['logged']=true;
         }

    //and then in profile.php

    if($_SESSION['logged']!=true)
        {
            
    header('Location: log_in.php');    
            exit();
        } 
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by English Breakfast Tea
    I realise that. How can I stop that in this situation?
    With isset(), just like I demonstrated.



    Originally Posted by English Breakfast Tea
    I think I got this from Northie, but I am not sure if it was him or E-oreo [...]
    This again suffers from the confusion regarding boolean values.

    I wonder why you think that a boolean stored in a variable somehow isn't "good enough" and needs to be put into a comparison. $_SESSION['logged'] already is either true or false. You can use it directly in any if statement or logical expression:

    PHP Code:
    if ($_SESSION['logged']) {...} 
    And the negation would be

    PHP Code:
    if (!$_SESSION['logged']) {...} 
    I mean, this almost reads like English: "If (the user) is not logged in, then ...".

    Booleans are real, actual values. They're just as real as any other value.

    Comparisons like $x == true or $x != true are actually nonsense, because PHP is a weakly typed language: If $x already is a boolean, then this code is just a weird typing exercise for the simple expressions $x and !$x. If it's not a boolean, PHP will convert it into one if necessary. And in the very, very rare occasions you need an explicit type conversion, you'd use casting: (boolean) $x or shorter !!$x

    Again: When you have expressions like $x == true or $x != true in your code, you're doing it wrong.
    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