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
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?

Link to comment
Share on other sites

Thanks,

 

I have included a username variable in my form.

 

I hadn't thought about what the code meant stupid me. How would I solve this error to make it work properly then?

 

Thanks again, the help is appreciated.

Link to comment
Share on other sites

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";
}

?>

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.