Jul
11th

Simple PHP & MySQL User Login System

Files under PHP & MySQL

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.

PHP_user_login

<?php

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.

PHP_user_login

PHP_user_login

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.

PHP_user_login

<html>
<head>
<title>
Login Form</title>
</head>
<body>

<form id=”loginFormname=”loginFormmethod=”postaction=”register-exec.php“>
<table width=”250border=”0align=”leftcellpadding=”2cellspacing=”0style=”font-family:Arial;font-size:11px“>

<tr>
<th>
First Name </th>
<td><input name=”fnametype=”textid=”fname” /></td>
</tr>

<tr>
<th>
Last Name </th>
<td>
<input name=”lnametype=”textid=”lname” /></td>
</tr>

<tr>
<th>
Login</th>
<td>
<input name=”logintype=”textid=”login” /></td>
</tr>

<tr>
<th>
Password</th>
<td>
<input name=”passwordtype=”passwordid=”password” /></td>
</tr>

<tr>
<th>
Confirm Password </th>
<td>
<input name=”cpasswordtype=”passwordid=”cpassword” /></td>
</tr>

<tr>
<td>&
nbsp;</td>
<td>
<input name=”Submittype=”submitvalue=”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

<?php

//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)

PHP_user_login

<html>
<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!!!



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

  1. By Matthew on Jul 14, 2009 | Reply

    Good article, love the screen shots! Looking forward to seeing Part 2 of this.

  1. 1 Trackback(s)

  2. Jul 18, 2009: Simple PHP & MySQL User Login System(Part 2) | CodeBasic.net

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