PHP中判斷輸入驗證碼是否一致

來源:互聯網
上載者:User

標籤: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";
}
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.