Jump to content

Undefined Index Problem


cactus

Recommended Posts

Hi,

 

I keep get this error message:

 

Undefined index: username in C:\wamp\www\admin.php on line 29

 

I have tried everything to resolve it and am now just frustrated.

 

Is there anyone who can help me out please?

 

My code where its having the problem is:

 

if (isset($_POST['Submit']) ||

    ($_POST['username'] == 'admin') &&  //THIS IS LINE 29

($_POST['password'] == 'xyz')) {

        $_SESSION['username'] = 'login';

    }

  else {

      echo "<b>You login details is not correct. Pls login again</b>";

    }

 

 

if ($_SESSION['username']=='login'){  //IT'S ALSO GIVING THE SAME ERROR FOR THIS LINE

  if (isset($_REQUEST['file'])) {

      $fc = file_get_contents($_REQUEST['file']);

      $text = explode("<!-- EDITABLE -->",$fc);

      echo "<form method='post' action=''><textarea name='content'>$text[1]</textarea>";

      echo "<p><input type='hidden' name='file' value='".$_REQUEST['file']."' /><input name='submitUpdate' type='submit' value='Update Page'></form>";

  }

Link to comment
https://forums.phpfreaks.com/topic/229997-undefined-index-problem/
Share on other sites

Look at your logic in this line:

if (isset($_POST['Submit']) || ($_POST['username'] == 'admin') && ($_POST['password'] == 'xyz')) {

 

It reads: "If the form is submitted, OR username == 'admin' . . ." If the form hasn't been submitted, how can 'username' equal anything at all?

Make sure your form has a username and password field and then use something like this:

 

<?php

session_start();

if (!isset($_SESSION['logged_in'])){
   // You're not logged in, have you submitted the form
   if (isset($_POST['Submit']) {
      // You have submitted the form
      if ($_POST['username'] == 'admin' && $_POST['password'] == 'xyz'){
         // You're authenticated
         $_SESSION['logged_in'] = $_POST['username'];
         echo "Thanks for logging in";
      }
      else {
         // You entered invalid details
         echo "Sorry, the username and password you entered are invalid";
      }
   }
}
else {
   // You're already logged in
   echo "You're logged in as " . $_SESSION['logged_in'];
}

if (!isset($_SESSION['logged_in'])){
   // Display the form
   echo "This is where your form code goes";
}

?>

Thanks Huggie Bear that looks fantastic and i'll try that out.

However I'm looking at my code again and am not sure the php tags and html tags are in the correct places.

Would someone mind looking for me as i'm not sure whether this is also causing a problem?

 

Thanks a lot its much appreciated.

 

[attachment deleted by admin]

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.