[HELP] How to do a simple login page? (PHP5)
Hi guys, I'm trying to make a login page and I've read dozen of tutorials on the Internet, but none of them worked. Whatever login/password I type, it says : 'Wrong Username or Password'.
I do not understand .PHP so much, but I'm learning how to use it properly...
My database:
http://img140.imageshack.us/img140/8973/27671750.jpg
main_login.php (post form)
PHP Code:
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
checklogin.php
PHP Code:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="9sm13nxa"; // Mysql password
$db_name="pw"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
loginsuccess.php
PHP Code:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="9sm13nxa"; // Mysql password
$db_name="pw"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Where are the errors? :x
Thanks in advance :}
Re: [HELP] How to do a simple login page? (PHP5)
Hi,
I hope i may still bump this.
Actually i was looking for a good way to prevent MySQL injection. Came along this, and found the right way.
But i was also interested in your question.
The problem is with your password.
As far as i can see, in your database you encrypted the password with MD5.
But in checklogin you do this:
checklogin.php
PHP Code:
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
Ofcourse, this will give the wrong username or password error.
If you submit the form, it will go like this (lets say, your password is "secret"):
PHP Code:
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; // $mypassword will equal "secret"!
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
//strip the slashes etc etc and your pass will still be "secret".
// But this is where it goes wrong!
//You just fill in "secret" in your query.
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
Your query will be something like: SELECT * FROM users WHERE username='tentasoh' and password='secret'
But in your database its something like: AJHSJHKJGFKHKJAJhKJAGKJ
Its md5 encrypted.
What you have to do is this:
PHP Code:
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; // $mypassword will equal "secret"!
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
//strip the slashes etc etc and your pass will still be "secret".
// Here we will encrypt the password!
$mypassword = md5($mypassword);
// Now your password equals something like: AKDSJASHDKJAHSDKJAHSFKJH
// And MySQL will compare the string to the string from the database.
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
This should fix your problem.
I hope it works and i am sorry for the long explanation, but i wanted to do it as easy as possible. I am not an expert in PHP, so correct me if i am wrong!
Thanks and good luck!
Cheers,
Clemenz