Here is the code for my login page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style type="text/css">
#apDiv1 {
    position:absolute;
    width:200px;
    height:115px;
    z-index:1;
    left: 608px;
    top: 201px;
}
#apDiv1 form table tr td {
    font-family: "Trebuchet MS";
}
</style>
</head>

<body>

<div id="apDiv1">
<form method ="post" action="login.php">
    <table width="300" border="0">
      <tr>
        <td width="134">Username:</td>
        <td width="156"><input type="text" name='username'/></td>
      </tr>
     <tr>
        <td><p>Password:</p></td>
        <td><p>
          <input type="password" name="password"/>
        </p></td>
      </tr>
      <tr>
      <td callspan"2"><input type="submit" name="submit" value="Login"/> </td>
      </tr>
          </p>

        </table>
  </form>
  </div>
  <?php
include"config.php";

mysql_connect($dbhost, $dbusername, $dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if($_POST["submit"]){

$myusername = ($_POST['username']);
$mypassword = ($_POST['password']);

if((!$myusername) || (!mypassword)){
    echo "Please fill in all fields!";
    exit;
}

    $sql = "SELECT * FROM {$dbtable} WHERE username ='{$myusername}' AND password='{$mypassword}'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if($count == 1){
        $_SESSION['username'] = $myusername;
        $_SESSION['password'] = $mypassword;
        $_SESSION['userrecord'] = mysql_fetch_assoc($result);

        echo "SUCCESSFUL! Click here to <a href='account.php'>Login</a>";

    }
}

?>

</body>
</html>

My output is telling me that I have an unidentified index here:

if($_POST["submit"]){

What does this mean and how do I fix this?

Thank you.

Dani AI

Generated

The "Undefined index" notice means your script is trying to read a key from a superglobal array that doesn't exist yet — in this case the form's submit field is being accessed before it was sent. The usual fix is to guard any access to $_POST (or $_GET) by checking that the request actually contains that key or that the HTTP method is POST, as suggested by . (php.net)

There are a few other problems to fix in the posted snippet that will both remove notices and make the login more reliable: the password check is missing the variable dollar sign (causes a notice and broken logic), session handling is used without calling the session starter first, and the page stores the raw password in session (unsafe). Start the session before you set/read $_SESSION and stop saving plaintext credentials. (php.net)

Security and longevity notes: the code uses the old ext/mysql functions which were deprecated and later removed — migrate to PDO or MySQLi and use prepared statements to avoid SQL injection. Don’t store cleartext passwords; store and verify hashes using PHP’s password-hashing API instead of comparing raw values. These changes are the most important for keeping the site working on modern PHP and for basic security. (php.net)

Quick checklist (apply in this order):

  • Only run login logic when the request is a POST or when the submit key exists.
  • Initialize and validate/sanitize inputs before using them.
  • Call session_start() before any output and never store plain passwords in $_SESSION.
  • Move DB code to PDO/MySQLi prepared statements and use password_hash()/password_verify().
    Fix those, and the undefined-index notice will disappear and the login will be much safer.

Recommended Answers

All 3 Replies

Error in title. Undefined Index*

Use if ( isset( $_POST['submit'] ) ) {

you're a sexy boy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.