March 12, 200917 yr Hi I am having trouble trying to create a log in and sign up form, the problem is happening when I try to use md5 to encrypt the code. HERE IS MY LOGIN CODE: <!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</title> </head> <body> <?php $new_user = $_POST["username"]; $new_password = md5($_POST["password"]); $con = mysql_connect("localhost","root","root"); if(!$con) { die('Cannot connect to Database' . mysql_error()); } mysql_select_db("login",$con); $sql = "SELECT * FROM users WHERE username='$new_user' AND password='$new_password'"; $result= mysql_query($sql,$con); $numrows = mysql_num_rows($result); $row = mysql_fetch_row($result); if($numrows ==0) { echo "Wrong username and password <br />"; }else { if($numrows ==1) { echo "Thank you for logging in <br />"; }else { echo "Not working <br />"; } } echo "Your name is " . $new_user . "<br />"; echo "Your password is " . $new_password . "<br />"; ?> <form action="login.php" method="post"> <h2> Please enter some notes </h2> <textarea name="notes"></textarea> <input type="submit" /> </form> <?php echo $_POST["notes"]; ?> </body> </html> HERE IS MY SIGN UP CODE: <!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</title> </head> <body> <?php $new_name = $_POST["username"]; $new_password = md5($_POST["password"]); $con = mysql_connect("localhost","root","root"); if(!$con) { die('Cannot connect to Database' . mysql_error()); } mysql_select_db("login", $con); mysql_query("INSERT INTO users (username, password) VALUES ('$new_name', '$new_password')"); mysql_close($con); ?> Welcome <?php echo $new_name;?><br /> You password is <?php echo $new_password;?> </body> </html> When I echo the password on both the log in script and sign up script it comes up with exactly the same md5 code and is also the same in PHPMYADMIN when I look under users password, so I am not sure what i am doing wrong if anyone has any ideas I would be very thankful Cheers
March 12, 200917 yr Author Well its coming up with wrong username and password, when I want it to say "Thank you for logging in". It worked fine without the md5 part but i want the password to be encrypted so I need it there, when I echo the variable $new_password it has the md5(password in it which seems right) however it still says "Wrong username and password". Am not sure what is wrong but as I am new to all this I can't figure it out.. ????
March 12, 200917 yr On both your pages, echo your mysql_query content just after executing it. You could do it like this:- $sql = "INSERT etc etc"; mysql_query($sql); echo($sql . '<br />'); Post back what is shown.
March 12, 200917 yr Author LOGIN: SELECT * FROM users WHERE username='admin' AND password='21232f297a57a5a743894a0e4a801fc3' Wrong username and password Your name is admin Your password is 21232f297a57a5a743894a0e4a801fc3 SIGNUP: Hasn't got a $sql... but when i echo for $new_password and $new_name Welcome admin You password is 21232f297a57a5a743894a0e4a801fc3 It has the same and when I SELECT the username and passwords FROM my database they are all the same that why im so confused... ???
March 12, 200917 yr Author Also here is my code for my login and signup form... <!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>Untitled Document</title> </head> <body> <form action="login.php" method="post"> <h1>Login Form</h1> Username:<input type="text" name="username" /><br /> Password:<input type="password" name="password" /><br /> <input type="submit" /> </form> <form action="signin.php" method="post"> <h1>Signin Form</h1> Username:<input type="text" name="username" /><br /> Password:<input type="password" name="password" /><br /> <input type="submit" /> </form> <br /> <br /> <a href="list_username.php">Click to see a list of all usernames and passwords</a> </body> </html>
March 12, 200917 yr In your login file, replace $result= mysql_query($sql,$con); $numrows = mysql_num_rows($result); $row = mysql_fetch_row($result); if($numrows ==0) { echo "Wrong username and password <br />"; }else { if($numrows ==1) { echo "Thank you for logging in <br />"; }else { echo "Not working <br />"; } } with if ($result = mysql_query($sql)) { $numrows = mysql_num_rows($result); echo($numrows . '<br />'); if ($numrows >= 1) { echo('welcome<br />'); } else { echo('user not found<br />'); } } else { echo('bad query<br />'); } OK I'm done now - try that.
March 12, 200917 yr Author Is still not working still coming up with this. 0 user not found Your name is admin Your password is 21232f297a57a5a743894a0e4a801fc3 I have just SELECTED the stored admin username and md5 password in the database and it seems to be 2 chararacters shorter than when the variable is echoed in the login and signup .phps?? Any other ideas im so lost !!! Thanks for he help by the way admin 21232f297a57a5a743894a0e4a801f
March 12, 200917 yr OK, 2 possibilities.... Firstly put some brackets in your SQL like this: $sql = "SELECT * FROM users WHERE (username='$new_user') AND (password='$new_password')"; Then if that doesn't work, rename your username and password fields. It's possible that "password" is a reserved word in MySQL. Call them something like "usernm" and "passwd". Make sure you change every occurance in the code, as well as renaming the fields in phpMyAdmin.
March 12, 200917 yr Author Hey, I have got it working now I was being stupid and had a limit of 30 chars in the sql database for password DUH!!! Sorry about that but thanks a lot for the help
March 12, 200917 yr Instead of double quotes $new_user = $_POST["username"]; $new_password = md5($_POST["password"]); Try single quotes $new_user = $_POST['username']; $new_password = md5($_POST['password']); Pat
March 12, 200917 yr Argh!! Oh well, we've all done it. Tip: while you're developing, near the top of each PHP page, put error_reporting(E_ALL); @Pat: on a default-ish PHP install, environment variables are fine with either style of quote (as long as they're matching of course...).
March 12, 200917 yr Author Thanks I will check that out. I have stumbled across another problem. I am trying to add to the code so you need to type somethign in both the username and password input boxes for it to send i currently have thisscript... $username = $_POST["username"]; $password = md5($_POST["password"]); //Adding the username and password to the database if($username == "" || $password == ""){ echo "You need to input something"; } else{ mysql_query("INSERT INTO user (username, password) VALUES ('$username', '$password')"); } The problem here is there is always a password as I am using md5 and even if the user has put nothing in the box the $password still has a value. Am wondering if there is anyway around this? Thanks again
March 12, 200917 yr Just move it up a bit... $bad = false; if ( ($_POST["username"] == "") || ($_POST["password"] == "") ) { $bad = true; } $username = $_POST["username"]; $password = md5($_POST["password"]); if ($bad) { echo('warning text'); } else { mysql_query("INSERT INTO user (username, password) VALUES ('$username', '$password')"); }
Create an account or sign in to comment