PHP+AjaxÑéÖ¤ÂëÑéÖ¤Óû§µÇ¼

5年以前  |  阅读数:974 次  |  编程语言:PHP 

ÓÃAJAX ÑeÖ¤Óû§µÇ¼µÄÒ»¸oºÃ´¦ÊDz»Ë¢ÐÂÌøתҳÃae£¬Ía¼ÓÓõ½ÑeÖ¤Âe¾Í¸u°²È«ÁË£¬ÃþË÷µÄдÁËÏ¡£Ò»¹²Óõ½Èý¸oÎļþ:

yz.php: Éu³ÉÑeÖ¤ÂeµÄPHP Îļþ£¬½«ÑeÖ¤Âe½«ÔÚSESSION Ài£¬¹(C)µÇ¼ʱ¶Ô±Èµ÷ÓÃ
index.php: Óû§µÇ¼µÄHTML Îļþ
loginCheck.php: ÑeÖ¤Óû§µÇ¼µÄÎļþ

ÏÂÃaeÒ»Ò»½aÎo:
yz.php Îļþ


    <?php
     session_start();

     //Éu³ÉÑeÖ¤Âeͼ
     Header("Content-type: image/PNG");
     //³¤Óe¿i
     $im = imagecreate(44,18);
     // ÉeÖñ³¾°É«:
     $back = ImageColorAllocate($im, 245,245,245);
     // Ìi³a±³¾°É«:
     imagefill($im,0,0,$back);

     srand((double)microtime()*1000000);
     $vcodes;
     //Éu³É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);
     }

     //¼ÓÈe¸ÉÈÅÏoËØ
     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);

     // ½«ËÄλµÄÑeÖ¤Âe±£´aeÔÚ SESSION Ài£¬µÇ¼ʱµ÷ÓöԱÈ
     $_SESSION["VCODE"]=$vcodes;
    ?>

index.php: ×¢Òa£¬ÔÚÕaÎļþÀi²»ÒªÈ¡ $_SESSION["VCODE"], *ñÔo»aÈ¡ÍiÒ»²½µÄ£¬Ë¢Ðºo²ÅÄÜÏÔʾÉÏÒ»¸oÑeÖ¤Âe

ÔÚ loginCheck.php ÀiÑeÖ¤¾ÍºÃÁË


    <!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" content="text/html;charset=gb2312">
    <title>¹ÜÀiºǫ| ÇeµÇ¼</title>
    <link rel="stylesheet" type="text/css" href="\css\a.css">
    <style type="text/css">
    <!--
      #main{
       font-family:ËÎÌa;
       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="";
     }

     // µa»÷ͼƬÖØлñµÃеÄÑeÖ¤Âe£º
     function getVCode() { 
      var vcode=document.getElementById('vcode'); 
      vcode.src ='yz.php?nocache='+new Date().getTime(); 
     }


     //¶¨ÒaXMLHttpRequest¶ÔÏo
     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 ¼i²eµÇ¼: ÓÐÃÜÂe£¬ÒªÓÃPOST Ìa½»
     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("ÇeÊaÈeÓû§ÃuºÍÃÜÂeºÍÑeÖ¤Âe£¡");
       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.onreadystatechange=function(){
         if(xmlHttp.readystate==4 && xmlHttp.status==200){
          var answer=xmlHttp.responseText;
          if(answer=="ok")                     //Ìøתµ½¹ÜÀiÖÐÐÄÒ³Ãae
           window.location.href="adminCenter.php";
          else{
           alert("Óû§ÃuÃÜÂe»oÑeÖ¤Âe²»ÕýÈ*! ÇeÖØÐÂÊaÈe!");
           document.getElementById('username').focus();
          }
        }
       }
      }
     }

    //-->
    </script>
    </head>
    <body onload="document.getElementById('username').focus();">
     <div id="main">
       <table>
         <tr>
         <td>Óû§Ãu:<input type="text" id="username" /></td>
         <td>ÃÜ   Âe:<input type="password" id="password" /></td>
         <td>ÑeÖ¤Âe:<input type="text" id="authCode" size="6" maxlength="4" value="ÑeÖ¤Âe" onfocus="clearX()"/></td>
         <td><img id="vcode" src="yz.php" alt="¿´²»Ça£¿µa»÷»»Ò»ÕÅ" onclick="getVCode()" /></td>
         <td><input id="loginButton" type="submit" value="µÇ ¼" onclick="login()"/></td>
         </tr>
        </table>
     </div>
    </body>
    </html>

loginCheck.php **ÑeÖ¤Óû§µÇ¼µÄÎļþ
**


    <?php 
     session_start();
     include("../conn/connDB.php");

     // È¡µÃPOST¹ýÀ´µÄ²ÎÊý:
     $username=$_POST["username"];
     $password=md5($_POST["password"]);
     $authCode=$_POST["authCode"];       

     $feedback="no";

    //¶Ô±ÈÊÇ*ñ==SESSIONÖеÄÑeÖ¤Âe£¬²»ÄÜ*ÅÔÚ¿Í»§¶Ë×o£¬*ñÔoÈ¡²»ÕýÈ*µÄÖµ
     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)                       // ÑeÖ¤³É¹¦
       $feedback="ok";
       $_SESSION["admin"]=true;           //ΪÁ˺ǫ°²È«£¬´aeÈeSESSION£¬±iÃ÷ ADMIN ÒѵǼ£¬¹(C)ºoÃaeµ÷ÓÃ
      }

     echo $feedback;

    ?>

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例