0

Hi All having a problem with the following statement
" Notice: Undefined index: Username in C:\wamp\www\david\remoteaccess\index.php on line 6". I have tried defining Username in a number of ways, but I seem to missing something.

Any help would be great

Thanks in advance

Thanks

<?php
session_start();
include("includes/utilities.php");
include("includes/functions.php");

$username = $_POST['Username'];
var_dump($_POST);

    $Errors = "";
    $_SESSION['LoggedIn'] = false;
    $_SESSION['UserID'] = "";
    $_SESSION['UserAdmin'] = false;
    $_SESSION['Username'] = $username; 

    if (isset($_POST["Username"])) {
        SQLUser();   
    }else{  
       loginForm();
    }


?>
4
Contributors
8
Replies
34
Views
4 Years
Discussion Span
Last Post by davidjennings
0

Sorry here is the form
Thanks

David

<form action="" autocomplete="off" id="Login" method="post" name="Login">
<input id="Submitted" name="Submitted" type="hidden" value="true" />

<label class="label-rounded" for="Username">Username</label><input class="input-text-rounded" id="Username" name="Username" type="text" value="<?= $_GET['Username']; ?>" /><br />

<label class="label-rounded" for="Password">Password</label>
<input class="input-text-rounded" id="Password" name="Password"type="password" />

<div class="centre mbottom">
<input class="button input-button-rounded shadow-small" id="LoginButton" name="LoginButton" type="submit" value="Login" /></div>
</form>
1

Try this

$username = (isset($_POST['Username'])) ? $_POST['Username'] : "";
1

The problem is, you're trying to set $username to $_POST['Username'] when you first load the page, but $_POST['Username'] has not yet been set.

You would want to set $username only if the form had been submitted.
Try something like:

<?php
session_start();
include("includes/utilities.php");
include("includes/functions.php");

    if (isset($_POST["Username"])) {

        $username = $_POST['Username'];

        var_dump($_POST);
            $Errors = "";
            $_SESSION['LoggedIn'] = false;
            $_SESSION['UserID'] = "";
            $_SESSION['UserAdmin'] = false;
            $_SESSION['Username'] = $username; 

        SQLUser(); 

    }else{
       loginForm();
    }
?>

This way it will not try setting $username unless the form has been submitted.

0

Hi both thanks for the reply it seems to still causing an error on line 39 which is in the login fuction beliw for the <?= $_GET['Username']; ?>

Thanks again for any help.

David

 function loginForm(){

?>

<form action="" method="post">
      <label class="label-rounded" for="Username">Username</label><input class="input-text-rounded" id="Username" name="Username" type="text" value="<?= $_GET['Username']; ?>" /><br />
      <label class="label-rounded" for="Password">Password</label><input class="input-text-rounded" id="Password" name="Password" type="password" />
      <div class="centre mbottom"><input class="button input-button-rounded shadow-small" id="LoginButton" name="LoginButton" type="submit" value="Login" /></div>
    </form>

<?php
 }
1

I would put in the form function the suggestion by Bachov Varghese

function loginForm(){
$username = (isset($_POST['Username'])) ? $_POST['Username'] : "";
?>
<form action="" method="post">
      <label class="label-rounded" for="Username">Username</label><input class="input-text-rounded" id="Username" name="Username" type="text" value="<?= $username ?>" /><br />
      <label class="label-rounded" for="Password">Password</label><input class="input-text-rounded" id="Password" name="Password" type="password" />
      <div class="centre mbottom"><input class="button input-button-rounded shadow-small" id="LoginButton" name="LoginButton" type="submit" value="Login" /></div>
    </form>
<?php
 }

Edited by pixelsoul

0

Thanks guys for your help $username = (isset($_POST['Username'])) ? $_POST['Username'] : ""; did the trick.

Thanks for your support

David

0

Just to clarify so you can avoid this in the future:

  1. This is just a Notice. Your code/script will still work. When your site is launched you'll probably want to change your error reporting settings to not show these.
  2. The Notice is telling you that the array location Username inside the array $_POST doesn't exist. With your original code, the script tries to access $_POST['Username'] and assign it to $username even if the user arrived on the page without submitting the form, in which case of course $_POST['Username'] would be nonexistant. By using isset() as indicated above, you check if this array index is even set, and if not your simply assign an empty string to the variable.

Honestly I think if makes more sense to rewrite like so:

<?php
session_start();
include("includes/utilities.php");
include("includes/functions.php");

if(isset($_POST["Username"])) { // add user input sanitation

    $username = $_POST['Username'];
    var_dump($_POST); // guessing this is just for debugging

    $Errors = "";

    $_SESSION['LoggedIn'] = false;
    $_SESSION['UserID'] = "";
    $_SESSION['UserAdmin'] = false;
    $_SESSION['Username'] = $username; 

    SQLUser();   

} else {
    loginForm();
}

EDIT: Aaaaaaaaaaaand once again I'm in after the buzzer ><

Edited by EvolutionFallen

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.