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();
    }


?>

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>

Try this

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

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.

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
 }

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
 }

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

Thanks for your support

David

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

EvollutionFallen - thanks for your reply

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.