Jump to content

Helping with admin login and user login


siabanie

Recommended Posts

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 appreciated.

Thanks.

 

login.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>

Link to comment
Share on other sites

Your issue is that you are getting 0 rows returned from the query.  This in turn points to the problem of your $username variable being either not something in the database, or wrongly populated.  Either way, it's not what you expect it should be, so echo the contents of $username and $password after it tells you that there is no such user and see what you get.

Link to comment
Share on other sites

There is no "correct" way to make a login form per-say.  There have been several example login forms posted on phpfreaks, each diffent and each correct for what it does.  You are better off trying to find what is causing the problems in your form and then work through fixing it.  It will make maintaining the finnished product much easier.  If you are looking for an out the box login form, check in the code repository section of the forum.  I'm sure there are quite a few in there.  If you want help fixing yours post up what you get back from that echo.

Link to comment
Share on other sites

Well I tried to echo the contents of $username and $password as you suggested then when I typed Username: admin and Password: 123

 

It didn't echo anything but output the same message "That user does not exist!" - I do not understand at all.

 

$username = $_POST['username'];
$password = $_POST['password'];
echo $username;
echo $password;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.