Hi guys,
Can anyone assist me. I am trying to create a login for admin and user (if user not a member click register link) below is my code: But whenever I enter the value as: Username: admin Password:123 – I got an error message “That user does not exist!”
Any suggestion and help would be highly appreciated.
Thanks.
login.php
[code=php]
<?php
//Assigned varibale $error_msg as empty
//$error_msg = “”;
session_start();
$error_msg = “”;
if (isset($_POST[‘submit’])) {
if ($a_username = “admin” && $a_password = “123”)
{
//Define $_POST from form text feilds
$username = $_POST[‘username’];
$password = $_POST[‘password’];
//Add some stripslashes
$username = stripslashes($username);
$password = stripslashes($password);
//Check if usernmae and password is good, if it is it will start session
if ($username == $a_username && $password == $a_password)
{
session_start();
$_SESSION[‘session_logged’] = ‘true’;
$_SESSION[‘session_username’] = $username;
//Redirect to admin page
header(“Location: admin_area.php”);
}
}
$username = (isset($_POST[‘username’])) ? $_POST[‘username’] : ”;
$password = (isset($_POST[‘password’])) ? $_POST[‘password’] : ”;
if($username && $password) {
$connect = mysql_connect(“localhost”, “root”, “”) or die (“Couldn’t connect!”);
mysql_select_db(“friendsdb”) or die (“Couldn’t find the DB”);
$query = mysql_query (“SELECT * FROM `user` WHERE username = ‘$username'”);
$numrows = mysql_num_rows($query);
if ($numrows != 0){
while ($row = mysql_fetch_array($query)) {
$dbusername = $row[‘username’];
$dbpassword = $row[‘password’];
}
//Check to see if they are match!
if ($username == $dbusername && md5($password) == $dbpassword) {
header (“Location: user_area.php”);
$_SESSION[‘username’] = $username;
}
else
$error_msg = “Incorrect password!”;
//code of login
}else
$error_msg = “That user does not exist!”;
//echo $numrows;
}
else
$error_msg = “Please enter a username and password!”;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Login Page</title>
</head>
<body>
<br />
<?php
require “header.php”;
?><br />
<div align=”center”>
<table width=”200″ border=”1″>
<?php
// If $error_msg not equal to emtpy then display error message
if($error_msg!=””) echo “<div id=”error_message”style=”color:red; “>$error_msg</div><br />”;?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>” method=”post”>
<!–form action=”login_a.php” method=”post”–>
Username: <input type=”text” name=”username” /><br /><br />
Password: <input type=”password” name=”password” /><br /><br />
<input type=”submit” name = “submit” value=”Log in” />
</form> <p> </p>
Register a <a href=”register.php”>New User</a>
</table>
</div>
</body>
</html>