Posted by:
nav2626
(---.nycmny.east.verizon.net)
hi...can smone please solve my this problem .... i'm not able to enter the IF statement its strightway going to else.....where after puttin the username correct its showin me
The username is invalid. It must be alphanumeric and 2-8 characters long
my code is:
<?php
session_start();
$con = mysql_connect("localhost","root","managed1"
or die ('Error connecting to mysql');
$dbname = 'minhas';
mysql_select_db($dbname,$con);
$loginerror = ""; // by default assume there is not login error
if (isset($_POST['login'])) {
// test for the correct username & password
if (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = strtolower($_POST['username']); // convert to lowercase to avoid upper/lowercase problems
if (ereg("^[a-z0-9]{2,8}$",$username)) { // validate username to protect from SQL injection attacks
// verify this username/password combination against a database
$sql = "SELECT * FROM customer WHERE username='".$username."'";
@mysql_query($sql) or die('Error in query: [<b>' . $sql . '</b>]. The error was: ' . mysql_error());
$result = $database->mysql_query($sql);
if ($result->numRows() > 0) {
$user = $result->fetchRow(DB_FETCHMODE_ASSOC,0);
if ( crypt($_POST['password'],$user['passwordcrypt'])==$user['passwordcrypt'] ) { // do password crypts match?
$_SESSION['username'] = $username; // remember the username
}
else {
$loginerror = "Wrong username or password. Please try again.";
}
}
else {
$loginerror = "Wrong username or password. Please try again.";
}
} // end: if (ereg("^[a-z0-9]{2,8}$",$username))
else {
$loginerror = "The username is invalid. It must be alphanumeric and 2-8 characters long.";
}
} // end: if (!empty($_POST['username']) && !empty($_POST['password']))
else {
$loginerror = "Please enter a username and password.";
}
} // end: if (isset($_POST['login']))
?>