2016-08-23 42 views
0

我想在圖形驗證碼從用戶完整的請求發送到一個網頁,並通過用戶與驗證碼圖像的值進行比較,輸入字符串(這個值存儲在會話值)通過Ajax請求與交變比較會話變量

這是頁面包含Ajax請求(在這裏就不img標籤,但它並不重要!):

<html> 
<body> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<input id="captcha" maxlength="5" onkeyup="myFunction()" value=""> 
<script> 
function myFunction() { 
    var x = document.getElementById("captcha"); 
    if(x.value.length==5){ 
     $(document).ready(function(){ 
     $.get("test.php", 
      function(data, status){ 
      alert("Data: " + data + "\nStatus: " + status); 
      }); 
     }); 
    } 
} 
</script> 
</body> 
</html> 

,這頁是得到POST請求:

<?php 
session_start(); 
$c=$_POST['captcha']; echo $c;echo gettype($c); 
$s=$_SESSION['captcha']; echo $s;echo gettype($s); 
if($c==$s) { 
    echo 'correct'; 
} else { 
    echo 'incorrect'; 
} 
?> 

但在回答:類型的會話變量是NULL!爲什麼?!

但是post變量的類型是字符串,但我確定session和post是相等的。

現在如何與一起比較?

+0

您需要更新名爲'myFunction'的函數,並且需要遵循實際的ajax請求系統... –

回答

0

此問題已解決!但我不怎麼確切!

我在serach ajax上工作,當然還有開始會話。

但它沒有問題之前和之後的工作。

但我認爲更好的是在發送請求,會話開始的頁面上。

它工作!

然後我刪除的session_start()語句,但它仍然工作:d

我覺得代碼沒有心情:))頁面發送請求的

代碼:

<html dir="rtl"> 
<body> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<img id="captcha" src="captcha.php" width="160" height="45" border="1" alt="CAPTCHA"> 
<small><a href="#" onclick=" 
document.getElementById('captcha').src = 'captcha.php?' + Math.random(); 
document.getElementById('captcha_code').value = ''; 
return false; 
">refresh</a></small> 
<input id="fname" type="text" name="captcha" size="6" maxlength="5" onkeyup="myFunction()" value=""> 
<small id="massage-captcha">input number show on the image.</small> 
<script> 
function myFunction() { 
var x = document.getElementById("fname"); 
if(x.value.length==5){ 
    document.getElementById("massage-captcha").innerHTML = "please wait!"; 
    $(document).ready(function(){ 
     $.post("test.php", 
     { 
     captcha: x.value 
     }, 
     function(data,status){ 
      if(data=="correct"){ 
       document.getElementById("massage-captcha").innerHTML = "correct";}else{ 
       document.getElementById("massage-captcha").innerHTML = "incorrect"; 
      } 
     }); 
    }); 
} 
else{ 
    b.value=""; 
} 

} 
</script> 

和文件調整請求:

<?php 
session_start(); 
if($_POST['captcha']==$_SESSION['captcha']){echo 'correct';} 
else{echo 'incorrect';} 
?> 

我最後的想法:服務器上的會話在過去的一天後被刪除,現在工作正常!