PHP用户验证 PHP+Ajax验证码验证用户登录

WBOY
发布: 2016-07-27 16:56:17
原创
1065 人浏览过

用AJAX 验证用户登录的一个好处是不刷新跳转页面,外加用到验证码就更安全了,摸索的写了下。一共用到三个文件:

yz.php: 生成验证码的PHP 文件,将验证码将在SESSION 里,供登录时对比调用
index.php:用户登录的HTML 文件
loginCheck.php: 验证用户登录的文件

下面一一解析:
yz.php 文件


登录后复制

index.php: 注意,在这文件里不要取 $_SESSION["VCODE"], 否则会取晚一步的,刷新后才能显示上一个验证码

在 loginCheck.php 里验证就好了

 

<meta http-equiv="Content-Type" c type="text/css" href="%5Ccss%5Ca.css">
<style type="text/css">
<!--
  #main{
   font-family:宋体;
   font-size:10pt;
   text-align:center;
   margin-top:510px;
  }
  
  body{
   background-attachment:fixed;
   background-position:center;
   background-image:url(./images/w2.jpg);
   background-repeat: no-repeat;
  }
  
  #authCode{background-Color:#F8F9FF;}
  
  table{text-align:center;}

//-->
</style>
<script type="text/javascript" src="./js/trim.js"></script>
<script type="text/javascript">
<!--

 function clearX(){
  document.getElementById('authCode').value="";
 }

 // 点击图片重新获得新的验证码:
 function getVCode() { 
  var vcode=document.getElementById('vcode'); 
  vcode.src ='yz.php&#63;nocache='+new Date().getTime(); 
 }


 //定义XMLHttpRequest对象
 var xmlHttp;     

 // 创建 XMLHttpRequest:
 function createXmlHttpRequest(){
 var xmlHttp=null;
 try{
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
 }catch(e){
  // Internet Explorer
  try{
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
 return xmlHttp;
 }

 // AJAX 检查登录: 有密码,要用POST 提交
 function login(){
  var authCode=trim(document.getElementById('authCode').value);
  var username=trim(document.getElementById('username').value);
  var password=trim(document.getElementById('password').value);
  if(username=="" || password=="" || authCode==""){
   alert("请输入用户名和密码和验证码!");
   return false;
  }else{
   if(!xmlHttp) xmlHttp=createXmlHttpRequest();
    var send_string="username="+username+"&password="+password+"&authCode="+authCode+"&fresh="+Math.random();
    xmlHttp.open("POST","loginCheck.php",true); 
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send(send_string); 
    xmlHttp.
     if(xmlHttp.readystate==4 && xmlHttp.status==200){
      var answer=xmlHttp.responseText;
      if(answer=="ok")                     //跳转到管理中心页面
       window.location.href="adminCenter.php";
      else{
       alert("用户名密码或验证码不正确! 请重新输入!");
       document.getElementById('username').focus();
      }
    }
   }
  }
 }

//-->
</script>

登录后复制
密 码: 验证码:  PHP+Ajax验证码验证用户登录

loginCheck.php  验证用户登录的文件

登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

以上就介绍了PHP用户验证 PHP+Ajax验证码验证用户登录,包括了PHP用户验证方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!