Today I’m going to teach you how to create a simple user login script with user levels by using PHP and MySQL. To start with this we will need a table in our database, which will hold the user records.
Features included:
- User Registration Form
- User Login Form
- User Logout Form
Step 1: Create the Database Configuration File (config.php)
You need to create a configuration file that stores the MySQL database connection settings. Creating a separate file for MySQL database connection settings is better since you may have to change your MySQL server, username, password or database in future.
define(‘DB_HOST‘,’localhost‘); //hostname
define(’DB_USER‘, ‘codebasic‘); //username
define(‘DB_PASSWORD‘, ‘123456‘); //password
define(’DB_DATABASE‘, ‘mydatabase‘); //database name
?>
Note: No need follow exactly. You can replace the settings above with your MySQL database settings.

Step 2: Create the User/Member Registration Form (register-form.php)
The first thing that we are going to do for the user registration front end module is to create a page where users can enter their personal particular details to login. I am not going to explain this basic HTML code in details, just assume that users have basic knowledge on how HTML works.
Here, I have created 5 input boxes named: first name, last name, login, password & confirm password.

<head>
<title>Login Form</title>
</head>
<body>
<form id=”loginForm” name=”loginForm” method=”post” action=”register-exec.php“>
<table width=”250” border=”0” align=”left” cellpadding=”2” cellspacing=”0” style=”font-family:Arial;font-size:11px“>
<tr>
<th>First Name </th>
<td><input name=”fname” type=”text” id=”fname” /></td>
</tr>
<tr>
<th>Last Name </th>
<td><input name=”lname” type=”text” id=”lname” /></td>
</tr>
<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>
<th>Confirm Password </th>
<td><input name=”cpassword” type=”password” id=”cpassword” /></td>
</tr>
<tr>
<td> </td>
<td><input name=”Submit” type=”submit” value=”Sign Up” /></td>
</tr>
</table>
</form>
</body>
</html>
Step 3: Create the User/Member Registration Script (register-exe.php)
The script that I created during this step will be executed after we submit the user registration form. Several key concepts will be illustrated in this step:
- Simple MySQL queries
- Including an external PHP file
//include database connection details
require_once(’config.php‘);
//Connect to MySQL server
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
//Select database
mysql_select_db(DB_DATABASE);
//Create INSERT query
$sql = “INSERT INTO members(firstname, lastname, login, passwd) VALUES(’”.$_POST['fname'].”‘,’”.$_POST['lname'].”‘,’”.$_POST['login'].”‘,’”.md5($_POST['password']).”‘)“;
$result = mysql_query($sql);
//Check whether the query was successful or not
if($result) {
header(”location: register-success.php“);
exit();
}
?>
Step 4: Create the User/Member Success Registration Form (register-success.php)

<head>
<title>Registration Successful</title>
</head>
<body style=”font-family:Arial;font-size:11px“>
<h3>Registration Successful !!!</h3>
<h5>Click<a href=”login-form.php“> here</a> to login.</h5>
</body>
</html>
Ok, now I feel a bit tired with coding, let us proceed to the user login form on next week. See you again!!!


By Matthew on Jul 14, 2009 | Reply
Good article, love the screen shots! Looking forward to seeing Part 2 of this.