siabanie Posted February 21, 2011 Share Posted February 21, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/ Share on other sites More sharing options...
Muddy_Funster Posted February 21, 2011 Share Posted February 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/#findComment-1177569 Share on other sites More sharing options...
siabanie Posted February 21, 2011 Author Share Posted February 21, 2011 Thanks Muddy_Funster for the feedback. Can you please tell me what is the correct way of doing this login form? Regards Siabanie Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/#findComment-1177572 Share on other sites More sharing options...
Muddy_Funster Posted February 21, 2011 Share Posted February 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/#findComment-1177580 Share on other sites More sharing options...
siabanie Posted February 21, 2011 Author Share Posted February 21, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/#findComment-1177590 Share on other sites More sharing options...
siabanie Posted February 21, 2011 Author Share Posted February 21, 2011 Thanks guys, I think I have work it out so far it seems okay - but still there is a few confusion between my forms. Cheers. Siabanie Quote Link to comment https://forums.phpfreaks.com/topic/228374-helping-with-admin-login-and-user-login/#findComment-1177646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.