Categories
PHP / MySQL

Ajax Login form using PHP and MySQL

Today I would like to show you Ajax login functionality implementation using PHP and MySQL. We gonna use MySQL PDO driver in this tutorial. PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code.

 Ajax Login form using PHP and MySQL

Create Ajax Login Form

Login Form

Create a file called login_form.php with following code.

Database Detials

This tutorial assuming that you have following user table structure in your database. For the sake of example script, I have provided insert query with test data.

You can generate password hash by using password_hash() function, example echo password_hash("password", PASSWORD_DEFAULT);.

Config.php

After importing above table into your database. Create a file called config.php, where you gonna save information about MySQL Data base configuration, you can use this file to save other configu

Login

Create a file called login.php with following code. This file will handle the login requests, It will validate user details against to the database. Upon valid and successful login, it will start the user session, otherwise, it will throw the appropriate error message.

secure.php

Create a file called secure.phpfile with following code. On successful login we will redirect user to this page. Only authenticated user can access this page. If unauthorized users try to access this page, users will forcefully redirected to login page.

logout.php

Create a file called logout.php file and put below code in the file.

Once everything is in place head over to the browser with http://localhost/login_form.php you should get output something like below image

Ajax Login form using PHP and MySQL
Ajax Login form using PHP and MySQL

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

2 replies on “Ajax Login form using PHP and MySQL”

Thank you very much.

i couldn’t run this
if(!password_verify($_POST[‘password’],$row[0][‘password’])) {

I was able to work like this
if($_POST[‘password’] != $row[0][‘password’]) {

Leave a Reply

Your email address will not be published. Required fields are marked *