標籤:c tar ext http a int
首先用session將隨機產生的驗證碼的值傳到頁面,然後擷取當前文字框中輸入的值 進行對比;代碼如下:
產生的隨機數,把它傳到session裡面
<?
session_start(); 必須要先開啟session開關。
$s = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
$r="";
for($i=1;$i<=4;$i++){
$r .= $s[rand(0,count($s)-1)];
}
$_SESSION[‘nb‘] = $r;
?>
然後寫Ajax代碼如下:
function checkYZM(){
getXmlhttp();
xmlHttp.open("GET","chekYZM.php?YZM="+yzm.value,"false");
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState ==4){
if(xmlHttp.status == 200){
var txt = parseInt(xmlHttp.responseText);
if(txt){
tS5.innerHTML = "驗證成功";
}else{
tS5.innerHTML = "驗證失敗";
}
}
}
}
xmlHttp.send(null);
}
最後在chekYZM.php頁面裡進行判斷:
<?php
session_start();
if(isset($_GET[‘YZM‘])){
$yzm = $_GET[‘YZM‘];
if($yzm == $_SESSION[‘nb‘]){
echo"1";
}else{
echo"0";
}
}
?>