-4

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Reference - What does this error mean in PHP?

Notice: Undefined index: ac in C:\xampp\htdocs\CMS\includes\login.php on line 6

Notice: Undefined index: logged in C:\xampp\htdocs\CMS\includes\login.php on line 13

I am receiving the above Notices with the below code. How can I define my indexes? I understand there is a way to use ISSET but I am not sure how to do it with $_SESSION logged and USERS; since there are multiple values. How would I clear up the above errors in a correct manner without just suppressing notices/errors?

<?php
session_start(); // initialize session
include($_SERVER['DOCUMENT_ROOT'] . 
        '/CMS/CMSController.php');

if ($_POST["ac"]=="log") { /// do after login form is submitted  
     if ($USERS[$_POST["username"]]==$_POST["password"]) { /// check if submitted username and password exist in $USERS array 
          $_SESSION["logged"]=$_POST["username"]; 
     } else { 
          echo 'Incorrect username/password. Please, try again.'; 
     }; 
}; 
if (array_key_exists($_SESSION["logged"],$USERS)) { //// check if user is logged or not  
    echo "You are logged in.";
    header('Location: /CMS/index.php');
} else { //// if not logged show login form 
     echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; 
     echo 'Username: <input type="text" name="username" />'; 
     echo 'Password: <input type="password" name="password" />'; 
     echo '<input type="submit" value="Login" />'; 
     echo '</form>'; 
}; 
?>
Community
  • 1
  • 1
ILikeTurtles
  • 1,012
  • 4
  • 16
  • 47

2 Answers2

2

You can check if something's check with isset() like you mentioned. Use it like this:

if(isset($_POST["ac"])) {
    //Code using ac here
}

This way, if there isn't anything at the ac index, the code using it will execute. isset itself won't cause the Notice.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
0

You can change it to something like this:

<?php
session_start(); // initialize session
include($_SERVER['DOCUMENT_ROOT'] . 
        '/CMS/CMSController.php');

// This is one way to do it - it will give AC a value if it does not exist
// Do this for each $_POST value that may NOT have a value when executing this
// PHP file, for example: $_POST['username'] and $_POST['password']

if (!isset($_POST['ac'])) $_POST['ac'] = FALSE;

OR

// Or you can do it this way
if(isset($_POST['ac'])) {

// The code below will only execute if AC is set

      if ($_POST["ac"]=="log") { /// do after login form is submitted  
         if ($USERS[$_POST["username"]]==$_POST["password"]) { /// check if submitted username and password exist in $USERS array 
          $_SESSION["logged"]=$_POST["username"]; 
       } else { 
            echo 'Incorrect username/password. Please, try again.'; 
       }; 
    };

 // Don't forget the closing curly brace. NO NEED to put ; after the }'s
 }

Either of these methods will work fine.

NYCBilly
  • 870
  • 1
  • 8
  • 11