How PHP implements the login function _php instance in Ajax way

Source: Internet
Author: User

This example describes the Ajax+php+mysql landing sample. Share to everyone for your reference. Specifically as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

1 login.php
In the login interface, JavaScript scripts asynchronously request dologin.php,dologin.php in AJAX mode responsible for user information validation (including verification code, PHP generated verification code can search itself). The code for the login interface is as follows:

<?php session_start ();? > <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

2 Ajaxhelper.js

function Createrequest () {
 try {
  request = new XMLHttpRequest ();
 } catch (tryms) {
  try {
   request = new ActiveXObject ("Msxml2.xmlhttp");
  } catch (otherms) {
   try {
    request = new ActiveXObject ("Microsoft.XMLHTTP");
   } catch (failed) {
    request = Null
 ;
 }}} 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.target;
 }
 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 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 succeeded! ', ' Code ': '". $_session["Code"]. "'}";
   echo "{' result ': true, ' info ': ' Login succeeded! '}";
   
   }
   else
   {
   echo ' {' result ': false, ' info ': ' Bad password! '} ';
   }
  }
  else
  {
   echo ' {' result ': false, ' info ': ' This user does not exist! '} ';
  }
 }
 else
 {
  echo ' {' result ': false, ' info ': ' Authenticode error! '} ';
>

4 conn.php

<?php $conn = $mysql _connect ("localhost", "root", "");
 mysql_select_db ("BBS", $conn);
 mysql_query ("SET NAMES GB2312"); ?> 5 <?php class Users {function users () {} function Checklogin ($username, $userpwd) {try {Mysql_conne
   CT ("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 ();
   The 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 |
  Deletion failed $name does not exist "; }} function UpdateUser ($id, $name, $pwd) {if is_numeric (intval (Trim ($id))) {if ($id && $name &&amp ;
    $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 user
     id= $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 the them is null. ";}} else {return False | $id is isn't type of int. ';}}?
 >

It has nothing to do with logins, 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 login function, I hope to help you learn.

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.