Learning notes: JS + simple PHP implementation of user registration and login

Source: Internet
Author: User

/*-----------------JS Code------------------------*/

<! DOCTYPE html>

<meta charset= "UTF-8" >

<title></title>

<script src= "Js/jquery-1.12.4.min.js" ></script>

<body>

<p><input type= "text" id= "username" placeholder= "user name"/></p>

<p><input type= "text" id= "pwd" placeholder= "password"/><br></p>

<input type= "button" id= "Regit" value= "register"/>

<script>

$ (function () {

var username = $ ("#username");

Username.blur (function () {

if (username.val () = = ") {

Alert ("User name cannot be empty");

}else{

Checkusername (Username.val ());

}

});

function Checkusername (name) {

$.ajax ({

Type: "Post",

URL: "demo1.php",

Async:true,

data:{"type": "Checkusername", "username": name},

Success:function (data) {

var flag = json.parse (data);

if (flag = = True) {

Alert ("Duplicate user name");

}else{

Alert ("User name is available");

}

}

});

}

})

$ ("#regi"). Click (function () {

var name = $ ("#username"). Val ();

var pwd = $ ("#pwd"). Val ();

$.ajax ({

Type: "Post",

URL: "demo1.php",

Async:true,

data:{"type": "Register", "username": Name, "Password":p wd},

Success:function (data) {

alert (data);

}

});

})

</script>

</body>


/*-----------------PHP Code------------------------*/

<?php

$type = $_post["type"];

if (Isset ($type)) {//exists as true

Check if the user name exists;

if ($type = = "Checkusername") {

$flag = "";

$username = $_post["username"];

$json = file_get_contents ("User1.json"); Get the data JSON format in User1.json

$arr _json = Json_decode ($json, true);//Convert JSON-formatted data to data of the array type

for ($i =0; $i <count ($arr _json); $i + +) {//Get array length

if ($arr _json[$i ["name"] = = $username) {

$flag = true;

}

}

echo Json_encode ($flag); echo returns the value of the foreground Json_encode converts the array type to JSON-type data

}

User registration; Writes a newly registered user name and password to the JSON file

if ($type = = "Register") {

$flag = true;

$username = $_post["username"];

$password = $_post["password"];

$array = Array ("name" = = $username, "pwd" = = $password);

$json = file_get_contents ("User1.json");

$arr _json = Json_decode ($json, true);

Array_push ($arr _json, $array);

$json = Json_encode ($arr _json);

File_put_contents ("User1.json", $json);

echo Json_encode ($json); //Return all users in the updated JSON file (for testing only)

}

The user is logged in, and the JSON file checks to see if the user name and password are entered correctly;

if ($type = = "Login") {

$name = $_post["username"];

$pwd = $_post["pwd"];

$flag = "";

$json = file_get_contents ("User.json");

$arr _json = Json_decode ($json, true);

for ($i =0; $i <count ($arr _json); $i + +) {

if ($arr _json[$i ["name"] = = $name && $arr _json[$i] ["pwd"] = = $pwd) {

$flag = true;

}

}

echo Json_encode ($flag);

}

}

?>


/*---------------------A JSON file-----------------------*/

[

{

"Name": "Admin",

"PWD": "1234"

}, {

"Name": "AAA",

"pwd": "3333"

}, {

"Name": "Q",

"PWD": "1112"

}, {

"Name": "111",

"pwd": "QQQ"

}, {

"Name": "1234567",

"pwd": "QQQ"

}

]


source code See attachment

This article is from the "11996687" blog, please be sure to keep this source http://12006687.blog.51cto.com/11996687/1860972

Learning notes: JS + simple PHP implementation of user registration and login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.