PHP使用者驗證 PHP+Ajax驗證碼驗證使用者登入

WBOY
發布: 2016-07-27 16:56:17
原創
1069 人瀏覽過

用AJAX 驗證使用者登入的一個好處是不刷新跳轉頁面,外加用到驗證碼就更安全了,摸索的寫了下。一共用到三個檔案:

yz.php: 產生驗證碼的PHP 文件,將驗證碼將在SESSION 裡,供登入時對比呼叫
index .php:使用者登入的HTML 檔案
loginCheck.php: 驗證使用者登入的檔案

下面一一解析:
yz.php 檔案

<&#63;php
 session_start();

 //生成验证码图
 Header("Content-type: image/PNG");
 //长与宽
 $im = imagecreate(44,18);
 // 设置背景色:
 $back = ImageColorAllocate($im, 245,245,245);
 // 填充背景色:
 imagefill($im,0,0,$back);

 srand((double)microtime()*1000000);
 $vcodes;
 //生成4位数字
 for($i=0;$i<4;$i++){
  $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
  $authnum=rand(1,9);
  $vcodes.=$authnum;
  imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
 }

 //加入干扰象素
 for($i=0;$i<100;$i++){
  $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
 }
  
 ImagePNG($im);
 ImageDestroy($im);

 // 将四位的验证码保存在 SESSION 里,登录时调用对比
 $_SESSION["VCODE"]=$vcodes;
&#63;>

登入後複製

index.php: 注意,在這文件裡不要取$_SESSION["VCODE"], 否則會取晚一步的,刷新後才能顯示上一個驗證碼

在loginCheck.php 裡驗證就好了

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" c type="text/css" href="\css\a.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>
</head>
<body  /></td>
     <td>密   码:<input type="password" /></td>
     <td>验证码:<input type="text" size="6" maxlength="4" value="验证码" /></td>
     <td><img src="yz.php" alt=" PHP+Ajax验证码验证用户登录"  /></td>
     <td><input type="submit" value="登 录" /></td>
     </tr>
    </table>
 </div>
</body>
</html>

登入後複製

loginCheck.php  驗證使用者登入的檔案

<&#63;php 
 session_start();
 include("../conn/connDB.php");
 
 // 取得POST过来的参数:
 $username=$_POST["username"];
 $password=md5($_POST["password"]);
 $authCode=$_POST["authCode"];       
 
 $feedback="no";

//对比是否==SESSION中的验证码,不能放在客户端做,否则取不正确的值
 if($authCode==$_SESSION["VCODE"]){

   $SQL="select * from users where username='$username' and password='$password'";
   $result=mysql_query($SQL);
   $rows=mysql_num_rows($result);
  if($rows==1)                       // 验证成功
   $feedback="ok";
   $_SESSION["admin"]=true;           //为了后台安全,存入SESSION,表明 ADMIN 已登录,供后面调用
  }
  
 echo $feedback;
 
&#63;>
登入後複製

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

以上就介紹了PHP用戶驗證 PHP+Ajax驗證碼驗證用戶登錄,包括了PHP用戶驗證的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!