PHP How to implement the login function via Ajax, Phpajax way to login _php tutorial

Source: Internet
Author: User

PHP How to implement the sign-in function by Ajax, Phpajax mode login


An example of Ajax+php+mysql landing is described in this article. Share to everyone for your reference. Specific as follows:
The results are as follows:

The specific code is as follows:

1 login.php
In the login interface, the JavaScript script asynchronously requests dologin.php,dologin.php to be responsible for user information verification (including verification code, PHP generated verification code can search by itself). The code for the login interface is as follows:

<?php session_start ();? >  
 
  Login 
 
                       Ajax PHP Demo System         
Author:wangming datetime:2009-9-1 version:1.0.0 email:wangmingemail@163.com
Name: wangming
E-commerce 06-2
©Copyright 2015.

2 Ajaxhelper.js

function createrequest () {try {  request = new XMLHttpRequest ();} catch (tryms) {  try {   request = new ActiveX Object ("Msxml2.xmlhttp");  } catch (otherms) {   try {    request = new ActiveXObject ("Microsoft.XMLHTTP");   } catch (failed) {    request = Nu ll;   }  } } return request;  function Getactivatedobject (e) {var obj; if (!e) {  //early version of IE  obj = window.event.srcElement;} else if (e.srcelement) {  //IE 7 or later  obj = e.srcelement;} else {  //DOM Level 2 browser  obj = E.targ Et } return obj; function addEventHandler (obj, EventName, handler) {if (document.attachevent) {  obj.attachevent ("on" + EventName, handler); } else if (Document.addeventlistener) {  Obj.addeventlistener (EventName, Handler, false);}}

3 dologin.php

<?php session_start (); Header ("content-type:text/html;charset=gb2312");//Prevent the return of Chinese garbled $name =$_post[' username ']; $pwd =$_post[' password ']; $imagecode =$_post[' code '; if (Strtoupper ($imagecode) ==$_session["code"]) {  include ("conn/conn.php");  $sql = "Select Studentname,studentpwd from tbstudent where studentid= '". $name. "'";  $result =mysql_query ($sql, $conn);  if ($row =mysql_fetch_assoc ($result))  {   if ($pwd = = $row [' studentpwd '])   {   $_session[' username ']=$ row[' Studentname '];   echo "{' result ': true, ' info ': ' Login successful! ', ' Code ': '". $_session["Code"]. "'}";   echo "{' result ': true, ' info ': ' Login successful! '} ';      }   else   {   echo ' {' result ': false, ' info ': ' Password wrong! '} ';   }  }  else  {   echo ' {' result ': false, ' info ': ' The user does not exist! '} ';  }} else {  echo ' {' result ': false, ' info ': ' CAPTCHA error !'}"; }? >

4 conn.php

<?php $conn = $mysql _connect ("localhost", "root", ""); mysql_select_db ("BBS", $conn); mysql_query ("SET NAMES GB2312")?> 5 <?phpclass Users {function Users () {} function Checklogin ($username, $USERPW   D) {try {mysql_connect ("localhost", "root", "123");   mysql_select_db ("Studentdb");   mysql_query ("SET NAMES GB2312"); $sql = "Select UserID from Tbuser where username= ' $username ' and userpwd= '". MD5 (Trim ($USERPWD)).   "'";   $result = mysql_query ($sql);    if ($result) {$arr = Mysql_fetch_row ($result);    $uid = $arr [0];     if ($uid! = "") {return "true| $uid login OK. $sql";    Mysql_close ();     } else {return "False|login failed! $sql";    Mysql_close ();    }} else {return "false| $result link db failed!";   Mysql_close ();   }} catch (Exception $ex) {return "false| $ex";  Mysql_close ();   }} function AddUser ($name, $pwd) {try {mysql_connect ("localhost", "root", "123");   mysql_select_db ("Studentdb");   mysql_query ("Set names gb2312"); $sql 0 = mysql_query ("Select UserID from Tbuser where username= ' $name '");   $info 0 = mysql_fetch_array ($sql 0);   $userid = $info 0[0]; if ($info 0! = False) {return ' false | $name is exisis.   (ID: $userid) ";   } $PWD = MD5 (Trim ($PWD));   $query = mysql_query ("INSERT into Tbuser (USERNAME,USERPWD) VALUES (' $name ', ' $pwd ')");   $error = Mysql_errno (); if ($query) {return ' true |   Add OK "; } else {return ' false |   $error "; }} catch (Exception $ex) {return ' false |  $ex ";  }} function DeleteUser ($name) {mysql_connect ("localhost", "root", "123");  mysql_select_db ("Studentdb");  mysql_query ("Set names gb2312");  $name = Trim ($name);  $sql 0 = mysql_query ("Select UserID from Tbuser where username= ' $name '");  $info 0 = mysql_fetch_array ($sql 0); if ($info 0! = False) {if (mysql_query ("Delete from Tbuser where username= ' $name '")) {return ' true | Delete OK. (ID: ".) $info 0[0].   ")"; } else {return ' false |   Delete Failed "; }} else {return ' false |  Delete failed $name not present "; }} function upDateuser ($id, $name, $pwd) {if (Is_numeric (Intval ($id))) {if ($id && $name && $pwd) {Mysql_    Connect ("localhost", "root", "123");    mysql_select_db ("Studentdb");    mysql_query ("Set names gb2312");    $PWD = MD5 (Trim ($PWD));    $isexists = mysql_query ("select * from Tbuser where userid= ' $id '"); if (mysql_fetch_array ($isexists)) {$sql 0 = mysql_query ("Update tbuser set username= ' $name ', userpwd= ' $pwd ' where useri     d= $id "); if ($sql 0) {return ture |     Update OK "; } else {return ' false |     Update Failed "; }} else {return ' false | Usrid= $id not exists. ";}} else {return ' false |id= $id name= $name and pwd= $pwd. At least one of them is null. ";}} else {return ' false | $id is not a type of int. ";}}}? >

Nothing to do with login, just to record how PHP implements curd operations on database MySQL.

The above is to tell you how PHP through the Ajax way to achieve the login function, I hope that everyone's learning is helpful.

http://www.bkjia.com/PHPjc/1074659.html www.bkjia.com true http://www.bkjia.com/PHPjc/1074659.html techarticle PHP How to implement the sign-in function by Ajax, Phpajax way to log In this example describes the Ajax+php+mysql landing examples. Share to everyone for your reference. The following: Operation effect ...

  • 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.