当前位置:Gxlcms > PHP教程 > PHP如何通过AJAX方式实现登录功能_php实例

PHP如何通过AJAX方式实现登录功能_php实例

时间:2021-07-01 10:21:17 帮助过:13人阅读

本文实例讲述了Ajax+PHP+MySQL登陆示例。分享给大家供大家参考。具体如下:
运行效果截图如下:

具体代码如下:

1 login.php
登录界面中,javascript脚本用ajax方式异步请求dologin.php,dologin.php负责用户信息验证(包括验证码,php生成验证码可以自行搜索).登录界面的代码如下:

<?php session_start();?>


 
 
 login
 
 
 
 
 
 
 
  
 
 
  
  
  
  
   Ajax PHP Demo System
   appcation storm image
   
   

Author:wangming DateTime:2009-9-1 Version:1.0.0 Email:wangmingemail@163.com
姓名:wangming
电商06-2
©Copyright 2015.

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");//防止返回的中文乱码
 $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':'登陆成功!','code':'".$_SESSION["code"]."'}";
   echo "{'result':true,'info':'登陆成功!'}";
   
   }
   else
   {
   echo "{'result':false,'info':'密码错误!'}";
   }
  }
  else
  {
   echo "{'result':false,'info':'该用户不存在!'}";
  }
 }
 else
 {
  echo "{'result':false,'info':'验证码错误!'}";
 }
?>

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_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");
   $sql0 = mysql_query("select userid from tbuser where username='$name'");
   $info0 = mysql_fetch_array($sql0);
   $userid = $info0[0];
   if ($info0 != 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);
  $sql0 = mysql_query("select userid from tbuser where username='$name'");
  $info0 = mysql_fetch_array($sql0);
  if ($info0 != false) {
   if (mysql_query("delete from tbuser where username='$name'")) {
    return "true | delete ok.(id:" . $info0[0] . ")";
   } else {
    return "false | 删除失败";
   }
  } else {
   return "false | 删除失败 $name 不存在";
  }
 }
 function UpdateUser($id, $name, $pwd) {
  if (is_numeric(intval(trim($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)) {
     $sql0 = mysql_query("update tbuser set username='$name',userpwd='$pwd' where userid= $id");
     if ($sql0) {
      return "ture | update ok";
     } else {
      return "false | 更新失败";
     }
    } 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 type of int.";
  }
 }
}
?>

与登录无关,只为了记录一下PHP如何在数据库MYSQL上实现CURD操作.

以上就是告诉了大家PHP如何通过AJAX方式实现登录功能,希望对大家的学习有所帮助。

人气教程排行