0

I am making a simple login form that saves to a database using MYSQL. I used a tutorial and at the end when I tested it I got an error saying

'Notice: Undefined index: login in C:\wamp\www\database_test\login.php on line 7'

Don't know what this means and after searching the internet still can't figure it out. Here is my code. Please help. Thanks!

 <?php

     session_name('MyLogin');
     session_start();
     session_destroy();

     if ($_GET['login'] == "failed") {    //<------This is line 7//
         print $_GET['cause'];
     }

 ?>
 <html>
     <body>
         <form name='login_form' method='post' action='log.php?action=login'>
             Login: <input type='text' name='user'><br />
             Password: <input type='password' name='pwd'><br />
             <input type='submit'>
         </form>
     </body>
 </html>

I basically copied this from a tutorial at

http://www.howtodothings.com/computers-internet/how-to-make-a-login-system-for-your-website

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Dylan Buth
  • 1,648
  • 5
  • 35
  • 57
  • 3
    "I basically copied this from a tutorial at" --- being a programmer is something more than just copy-and-paste you know... That tutorial was written by newbies, that's why you get errors – zerkms Jan 02 '12 at 05:25
  • so since you admitted to copy and pasting I'm not going to just give you the answer :) I will give you a hint though, take a look at your input names and the $_GET index names. Hopefully that will help – Robert Jan 02 '12 at 05:28
  • 1
    I didn't copy and paste. – Dylan Buth Jan 02 '12 at 05:29
  • asked so many times before, please search first. –  Jan 02 '12 at 05:30

3 Answers3

2

This notice happens when you're trying to access the element that doesn't exist in array. To check if it does - use isset()

if (isset($_GET['login']) && $_GET['login'] == "failed") {
zerkms
  • 249,484
  • 69
  • 436
  • 539
2

Dylan, I highly suggest that you RTM. If you don't understand the undefined index error, it's probably not a good idea to start writing code just yet.

This is thrown on line 7 (like the error states) at $_GET['login']. This error was thrown because login is not a key found in the $_GET array. Therefore, PHP throws the notice. You can add an isset() or !empty() check to avoid the error, like such:

if (!empty($_GET['login']) && $_GET['login'] == "failed" )
Kenaniah
  • 5,171
  • 24
  • 27
0

Suppose you run your code at http://localhost/yoursite

If your give the browser the address like this. No error will occur. Because you've assigned to $_GET['login'] the value "failed".

http://localhsot/yoursite/index.php?login=failed

But if you don't provide "login=failed" in the address

http://localhost/yoursite/index.php

$_GET['login'] is not set, and php cannot find 'login' index in $_GET array, it throws an error :)

The solution to this is to check if $_GET['login'] is set before checking its value.

if ( !empty($_GET['login']) && $_GET['login'] == "failed" )

empty($_GET['login']) returns true if $_GET['login'] is NOT set

Hieu Nguyen
  • 1,497
  • 3
  • 12
  • 15