Forum Moderators: coopster

Message Too Old, No Replies

Undefined index on line etc...

the above is resulting from login...

         

henry0

8:40 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot figure what goes wrong
I tried it on 2 servers one w/ reg glob on and one with the glob off
same result
" Undefined index:..line 12"
&
" Undefined index:..line 13"

( I numbered the 2 lines)
Thank you
Henry
BTW do I really need to start a session when using $_SESSION, doesn't PHP make it automatic?

<?
$db = mysql_connect("foo", "foo", "foo") or die ("Couldn't connect to the database.");
mysql_select_db("foo") or die("Couldn't select the database");

// Add slashes to the username, and make a md5 checksum of the password.

/*LINE 12 */
$_POST['username'] = addslashes($_POST['username']);
/*LINE 13 */
$_POST['userpass'] = md5($_POST['userpass']);

$result = mysql_query("SELECT count(userid) FROM members WHERE userpass='$PHP_AUTH_PW' AND
username='$PHP_AUTH_USER'") or die("Couldn't query the user-database.");
$num = mysql_result($result, 0);

if (!$num) {

// if the query didn't return anything, // display the login form.

echo "<h3>User Login</h3>
<form action='$_SERVER[PHP_SELF]' method='post'>
Username: <input type='text' name='username'><br>
Password: <input type='password' name='userpass'><br><br>
<input type='submit' value='Login'> </form>";

}
else
{

// Start the login session

// We've already added slashes and MD5'd the password
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['userpass'] = $_POST['userpass'];

echo "<h1>Congratulations</h1>";
echo "You're now logged in. <a href='page2.php'>Page 2</a>.";

}

?>

Birdman

8:47 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like the post data didn't make it to the page.

Have you tried..

print_r($_POST);

...to see what exactly was posted?

henry0

9:01 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



very good idea
now we are moving along:
here is the result
Array ([username] => [userpass] => " and then the ash")
<<<
edit:
But I still do not figure where that leads me
>>>

coopster

10:43 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The first time you call up your script, the $_POST array won't be populated, so PHP is throwing a NOTICE error telling you that you are trying to use indexes that don't exist yet (username,userpass). A good practice is to make sure the variable is set before trying to modify it:
$username = (isset($_POST['username']))? $_POST['username'] : ''; 
$userpass = (isset($_POST['userpass']))? $_POST['userpass'] : '';

Then use those variables within the rest of your script.

coopster

10:48 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Another way of processing form variables is to check if the submit button was actually activated before attempting to use $_POST variables:

// Make sure you give your submit button a name:
<input type='submit' name='process' value='Login'>
--
-- then you can check to see if a submit was requested:
--
if (isset($_POST['process'])) {
// process my form
}

henry0

11:43 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Coopster
done making sure the variable is set was the reason for throwing those errors
Thank you

Henry

<<edit
BTW, indeed your solution is more elegant
just before I also figured that the var was empty
so I tried to remove the form part and first send
my data within the login.php
which did OK, but was more one "Spaghetti side :)"
>>>