|
Subject:
|
Notice: Undefined index: username in
|
|
Posted By:
|
latinquarter
|
Post Date:
|
4/14/2008 12:52:19 AM
|
Hi all,
im not sure if this is the correct place to post so if it is not, please direct it to where it supposed to be.
Im getting this error:
Notice: Undefined index: username in /clientdata/clients/x/b/xbs.com.au/www/stafflogin/login.php on line 6
and this is my coding:
<?php include('db.php'); include('sess.php'); session_start(); include('db.php'); if ($_POST['username'] && $_POST['password']) { //<-- line 6 $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $query = "SELECT * FROM users WHERE username='" . $username . "' AND password='" . $password . "'"; $result = mysql_query($query); if (!$result) { echo "Could not successfully run query ($query) from DB: " . mysql_error(); exit; } $site = dirname($_SERVER['SCRIPT_NAME']); $site = str_replace('/','',$site); $user = mysql_fetch_assoc($result);
if ($user && $user['site_name']==$site){ $_SESSION['user']['id'] = $user['id']; $_SESSION['user']['username'] = $user['username']; $_SESSION['user']['group_id'] = $user['group_id'];
it continues but the error is on line 6.
IF anyone could help me out with this problem it would be greatly appreciated..
Cheers.
|
|
Reply By:
|
sdi126
|
Reply Date:
|
4/18/2008 11:37:08 AM
|
Its because the first time you load the page nothing has been posted so it doesn't know what username is. Also that isn't really an error that would stop the program...just a notice message (this means you have your error reporting level set to show all types of messages)...warnings,notices,errors...etc
Change your code to this:
<?php
include('db.php');
include('sess.php');
session_start();
include('db.php');
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if ($username != '' && $password != '') { //<-- line 6
$username = mysql_real_escape_string($username);
$password = md5(mysql_real_escape_string($password));
$query = "SELECT * FROM users WHERE username='" . $username . "' AND password='" . $password . "'";
$result = mysql_query($query);
if (!$result) {
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$site = dirname($_SERVER['SCRIPT_NAME']);
$site = str_replace('/','',$site);
$user = mysql_fetch_assoc($result);
if ($user && $user['site_name']==$site){
$_SESSION['user']['id'] = $user['id'];
$_SESSION['user']['username'] = $user['username'];
$_SESSION['user']['group_id'] = $user['group_id'];
http://arizonawebdevelopment.com
|