- Ajax Login form using PHP and MySQL
- Create Ajax Login Form
- Login Form
- Database Details
- Config.php
- Login
- secure.php
- logout.php
- Login with ajax in PHP, MySQL
- Steps to Create Login Form using Ajax, PHP
- 1. Create Ajax Login Directory
- 2. Create a Database Connection
- 3. Create a Login Form
- Ajax Login Form
- 4. Send Login Request with Ajax
- 5. Get Login Response with PHP & MySQL
- 6. Create a Dashboard with User Details
- Welcome to Dashboard
- 7. Get Logged in User Details
- 8. Create Logout in PHP
- 9. Test Ajax Login Yourself
- Related posts:
- Login page with jQuery and AJAX
- Contents
- 1. Table structure
- 2. Configuration
- 3. HTML
- 4. PHP
- 5. jQuery
- 6. Demo
- 7. Conclusion
- JQuery, Ajax и общение с пользователями
- 1) Сss
- 2) PHP скрипт, который все это обрабатывает
- И собственно Ajax скрипт (с помощью фреймворка JQuery)
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 the 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.
Create Ajax Login Form
Login Form
Create a file called login_form.php with the following code.
Login Page
Login Database Details
This tutorial assumes that you have the following user table structure in your database. For the sake of the example script, I have provided an insert query with test data.
CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2; -- -- Dumping data for table `users` -- INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `email`, `password`, `date_added`) VALUES (1, 'Arjun', 'PHP', '[email protected]', '$2y$10$8mVSGv/bIGgcvCikXBPfTu7HfXMl3jqfiirtQGyRwV5bvOzNGmmLG', '2017-10-12 18:09:10');
You can generate password hash by using password_hash() function, example echo password_hash(«password», PASSWORD_DEFAULT); .
Config.php
After importing the above table into your database. Create a file called config.php, where you gonna save information about MySQL Database configuration, you can use this file to save other config details too.
Login
Create a file called login.php with the following code. This file will handle the login requests, It will validate user details against the database. Upon valid and successful login, it will start the user session, otherwise, it will throw the appropriate error message.
if (empty($_POST['password'])) < $error[] = "Password field is required"; >if (!empty($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) < $error[] = "Enter Valid Email address"; >if (count($error) > 0) < $resp['msg'] = $error; $resp['status'] = false; echo json_encode($resp); exit; >$statement = $db->prepare("select * from users where email = :email"); $statement->execute(array(':email' => $_POST['email'])); $row = $statement->fetchAll(PDO::FETCH_ASSOC); if (count($row) > 0) < if (!password_verify($_POST['password'], $row[0]['password'])) < $error[] = "Password is not valid"; $resp['msg'] = $error; $resp['status'] = false; echo json_encode($resp); exit; >session_start(); $_SESSION['user_id'] = $row[0]['user_id']; $resp['redirect'] = "dashboard.php"; $resp['status'] = true; echo json_encode($resp); exit; > else
secure.php
Create a file called secure.php file with the following code. On successful login, we will redirect the user to this page. Only authenticated users can access this page. If unauthorized users try to access this page, users will forcefully be redirected to the login page.
logout.php
Create a file called logout.php and put the 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 the below image
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face — we are here to solve your problems.
Login with ajax in PHP, MySQL
The Ajax Login form is very useful to sign in quickly to the database without refreshing the web page. So. It is frequently used in most web applications to make their registration form userfriendly & attractive.
In this tutorial, I have shared some simple steps to create the login/sign-in form using jquery Ajax in PHP & MySQL.
Steps to Create Login Form using Ajax, PHP
Now, Let’s start the coding to create the Ajax login form with the following simple steps –
1. Create Ajax Login Directory
First of all, You should create the following directory structure to build the login form.
source-code/ |__database.php |__login.php |__dashboard.php |__user-details.php |__logout.php |__login.js
2. Create a Database Connection
3. Create a Login Form
To create the Login form UI, You have to follow the following points –
- Create a basic HTML structure
- Include the bootstrap5 CDN
- Create a form with id ‘loginForm’
- Also, create some input fields for emailAddress, password & submit button.
- Include the jQuery CDN
- Include the login.js
Ajax Login Form