PHP User Authentication with MySQL

In this article, we are going to deal with user authentication mechanism using PHP and MySQL. Using this mechanism, we can protect our website from anonymous access.

For that, we need to create a login panel to let users to send their login information. And then, these information will be compared with the registered user list stored into the database. If match found, then the user will be considered as an authorized user.

Steps to create user authentication system in PHP.

  1. Create MySQL database with users table.
  2. Create user login panel.
  3. Generate query to compare user input with database.


Create MySQL database with users table.

We can create new database using MySQL query or by using any database client like SQLYog, phpmyadmin and etc. Here, the database named ‘payload’ is created and users table is inserted in to it with list of user entries as shown below.


Create user login panel.

We need to create login panel using HTML and css to let user to enter their username and password. So the HTML content corresponding to the login panel is,

<title>User Login</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<form name="frmUser" method="post" action="">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
<tr class="tableheader">
<td align="center" colspan="2">Enter Login Details</td>
<tr class="tablerow">
<td align="right">Username</td>
<td><input type="text" name="userName"></td>
<tr class="tablerow">
<td align="right">Password</td>
<td><input type="password" name="password"></td>
<tr class="tableheader">
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td>

And the css is,

.tableheader {
background-color: #95BEE6;
.tablerow {
background-color: #A7D6F1;
.message {
color: #FF0000;
font-weight: bold;
text-align: center;
width: 100%;

By adding the above styles, user authentication form will look like,


Generate query to compare user input with database.

After designing login panel using HTML and css, now we need to generate query to compare the user input sent through the form with the user table entries shown in the above diagram

So, we must connect the server and select database to execute the required query to perform user authentication. This will be implemented by adding the following code, on top of the HTML content shown in previous step and save this file as login.php.

if(count($_POST)>0) {
$conn = mysql_connect("localhost","root","");
$result = mysql_query("SELECT * FROM users WHERE userName='" . $_POST["userName"] . "' and password = '". $_POST["password"]."'");
$count  = mysql_num_rows($result);
if($count==0) {
$message = "Invalid Username or Password!";
} else {
$message = "You are successfully authenticated!";

PHP code will be executed only if any input is posted from the user form on submit. Otherwise, this file will simply return the HTML form to the browser.

Download PHP User Authentication with MySQL Source Code

This PHP code tutorial was published on May 7, 2013.

↑ Back to Top