search
HomeBackend DevelopmentPHP ProblemHow to use php+AJax+json to implement login verification

With the development of WEB2.0 and AJAX, more and more sites use AJAX technology to asynchronously load some pages. As a popular web development language, PHP can achieve some cool effects when combined with AJAX. This article will introduce how to use AJAX and JSON to implement basic login verification functions.

First we need to prepare the following files: index.html, login.php, user.json. Among them, index.html is the homepage of the website, login.php is used to process login requests, and user.json is used to store user information.

1. Design of index.html

In index.html we need to design a login form, which consists of two fields: username and password. To facilitate AJAX calls, you can add the id attribute to the form.

nbsp;html>


    <meta>
    <title>登录</title>
    <script></script>


    
        
        
             
    
    <script></script>

2. Writing of login.php

login.php is mainly used to process login requests. The login request needs to determine whether the username and password are correct. If they are correct, a string in JSON format will be returned to indicate successful login. Otherwise, a login failure message will be returned.

<?php header(&#39;Content-Type: application/json&#39;);
$data = json_decode(file_get_contents(&#39;../data/user.json&#39;), true);
$username = $_POST[&#39;username&#39;];
$password = $_POST[&#39;password&#39;];
if ($username === $data[&#39;username&#39;] && $password === $data[&#39;password&#39;]) {
    $result = array(&#39;status&#39; => 1, 'msg' => '登录成功');
} else {
    $result = array('status' => 0, 'msg' => '用户名或密码错误');
}
echo json_encode($result);

3. Writing of user.json

user.json stores user name and password information. This file can be generated in various ways, such as manually written, exported from a database, etc.

{
    "username": "admin",
    "password": "123456"
}

4. Writing of login.js

login.js is mainly used to process the submission request of the login form and send the form data to login.php through AJAX. , the login result is returned to the page through the callback function.

$(function(){
    $('#login-btn').click(function(){
        $.ajax({
            type: 'POST',
            url: 'login.php',
            data: $('#login-form').serialize(),
            dataType: 'json',
            success: function (data) {
                if (data.status === 1) {
                    $('#msg').html(data.msg).css('color', 'green');
                } else {
                    $('#msg').html(data.msg).css('color', 'red');
                }
            }
        });
    });
});

In the above code, first we get the ID of the login button through the jQuery selector, and then call the AJAX method in the click event. In the AJAX method, we define the request type and address, as well as the data to be sent to login.php, and specify the data type as JSON.

In the callback function, we perform logical operations based on the returned data. If the login is successful, a success message is displayed, otherwise a failure message is displayed.

At this point, a basic login verification function has been implemented. Through the collaboration of AJAX and JSON, we can achieve a more efficient Web development model, making the user experience smoother and developers more efficient.

The above is the detailed content of How to use php+AJax+json to implement login verification. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools