I'm getting two errors detailed below:

Notice: Undefined index: username in C:\wamp\www\member.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0006  131808  {main}( )   ...\member.php:0

( ! ) Notice: Undefined index: passwd in C:\wamp\www\member.php on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0006  131808  {main}( )   ...\member.php:0

And my code is:

<?php

// include function files for this application
require_once('require_fns.php');
session_start();

//create short variable names
$username = $_POST['username'];
$passwd = $_POST['passwd'];

if ($username && $passwd) {
// they have just tried logging in
  try  {
    login($username, $passwd);
    // if they are in the database register the user id
    $_SESSION['valid_user'] = $username;
  }
  catch(Exception $e)  {
    // unsuccessful login
    do_html_header('Warning:');
    echo 'You have not filled the form out correctly.
          Please go back and try again.';
    //do_html_url('login.php', 'Login');
    do_html_footer();
    exit;
  }
}

do_html_header('Home');
check_valid_user();

// give menu of options
display_user_menu();

do_html_footer();
?>

And I don't know how to fix it and I've been stuck on it for some time.

Recommended Answers

All 6 Replies

Member Avatar for RudyM

I would print_r($_POST) to see what's in your POST array. What does your form look like?

Member Avatar for Zagga

Hi,

You are trying to set $_POST['username'] and $_POST['passwd'] as variables without checking if they actually exist.

I would alter lines 8, 9 and 11 of your code slightly:

if ($_POST['username'] && $_POST['passwd']){
$username = $_POST['username'];
$passwd = $_POST['passwd'];

This way you are checking if the post variables exist before assigning them to variables.

I have declared the variables in the register_new.php script as $username=$_POST['username'];
$passwd=$_POST['passwd'];

I have declared the variables in the register_new.php script as $username=$_POST['username'];
$passwd=$_POST['passwd'];

The problem is not $username. The problem is $_POST['username']. If your page is hosted at yoursite.com/member.php, when you type that url directly onto the browser's address bar you are submitting a GET request (or if you have a <form> that does not explicitly specify method="post", it defaults to a GET request), not a POST request. Yet, line 8 of your script assumes that there is a field/key named username in$_POST. You need to test to see if username exists in $_POST before you attempt to use it. Try:

<?php
// include function files for this application
require_once('require_fns.php');
session_start();

//create short variable names
$username = null;
$passwd   = null;

if( array_key_exists('username', $_POST) )
{
    $username = $_POST['username'];
}

if( array_key_exists('passwd', $_POST) )
{
    $passwd = $_POST['passwd'];
}

if ($username && $passwd) {
// they have just tried logging in
  try  {
    login($username, $passwd);
    // if they are in the database register the user id
    $_SESSION['valid_user'] = $username;
  }
  catch(Exception $e)  {
    // unsuccessful login
    do_html_header('Warning:');
    echo 'You have not filled the form out correctly.
          Please go back and try again.';
    //do_html_url('login.php', 'Login');
    do_html_footer();
    exit;
  }
}
do_html_header('Home');
check_valid_user();
// give menu of options
display_user_menu();
do_html_footer();
?>
commented: Post gets +1 +10

Thanks, it's working now :)

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.