Jump to content

Notice About not Defined Index with Login Form


Glese

Recommended Posts

<?php

$user_name = 'bazarr';
$user_password = '*******';


if($_POST['user_name'] != $user_name || $_POST['user_password'] != $user_password) {

?>

<h3>The Login</h3>
<form method="post" action="">
    <input type="text" name="user_name" />
    <input type="password" name="user_password" />
    <input type="submit" name="blog_creation_login_submit" value="Submit" />
</form>

<?php
} else {
?>




<h3>The Article</h3>

 

With the above script I am getting following notice:

 

Notice: Undefined index: user_name

 

It is regarding the user name variable, my question is how to solve this one and avoid the notice?

You avoid the notice by checking to see if the variable exists. 

 

Index, in this context, means array index.  The array in question is $_POST.  It's telling you that $_POST['user_name'] doesn't exist; or, more specifically, 'user_name' is an undefined index because it wasn't set during the POST because whatever form input it's supposed to come from was empty.

 

So, you need to ensure that the variable exists by checking either isset or empty before attempting to use it in another way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.