Jul
18th

Simple PHP & MySQL User Login System(Part 2)

Files under PHP & MySQL

This is a continued lesson of last week tutorial Simple PHP & MySQL User Login System(Part 1).

Step 5: Create the User Login Form (login-form.php)

phpuserlogin
I have created the login form for user to enter their login and password. It includes 2 fields “login” and “password” and submit button “Login”.

<html>
<head>
<title>Login Form</title>
</head>
<body>
<form id=”loginForm” name=”loginForm” method=”post” action=”login-exec.php”>

<table width=”300″ border=”0″ align=”center” cellpadding=”2″ cellspacing=”0″ style=”font-family:Arial;font-size:11px”>
<tr>
<th>
Login</th>
<td>
<input name=”login” type=”text” id=”login” /></td>
</tr>
<tr>
<th>
Password</th>
<td>
<input name=”password” type=”password” id=”password” /></td>
</tr>
<tr>

<td></td>
<td>
<input type=”submit” name=”Submit” value=”Login” /></td>
</tr>
</table>

</form>
</body>
</html>

Step 6: Create the User Login Script (login-exec.php)

This step is to do validation checking after you submit the form. It will check the login against the database to ensure that the correct information was entered. If it is wrong, then it will redirect to login-failed.php.

<?phprequire_once(’config.php’);

//Connect to mysql server and select database
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

//Create sql query
$sql=“SELECT * FROM members WHERE login=’”.$_POST['login'].”‘ AND passwd=’”.md5($_POST['password']).”‘”;
$result=mysql_query($sql);

//Check whether the query was successful or not
if($result) {

if(mysql_num_rows($result) == 1) {

//Login Successful
header(“location: login-success.php”);
exit();

}else {

//Login failed
header(“location: login-failed.php”);
exit();

}

}
?>

Step 7: Create the User Success Login Page (login-success.php)

phpuserlogin

<div style=”font-family:Arial;font-size:18px” align=”center”>
Welcome <br>
<a href=”login-form.php”>Back</a>
</div>

Step 8: Create the User Failed Login Page (login-failed.php)

<div style=”font-family:Arial;font-size:18px” align=”center”>Login Failed!</div>

However, this is just a simple code and I did not do something to prevent sql injection. I will provide more advanced tutorial in future. So, keep in touch!!!



2 Responses to “Simple PHP & MySQL User Login System(Part 2)”

  1. By How I Lost 30 Pounds in 30 Days Without Diet on Jul 24, 2009 | Reply

    Thanks for posting about this, I would like to read more about this topic.

  2. By ChewTheobby on Oct 14, 2009 | Reply

    I usually don’t post in Blogs but your blog forced me to, amazing work.. beautiful …

Sorry, comments for this entry are closed at this time.