2017-08-25 37 views
1

使用下面的代碼,我想要一個頁面,當它第一次加載時,它要求進行驗證碼驗證,然後(當會話有效時)它不再需要它。 當我在使用recaptcha驗證後點擊打開管理工具時,它會顯示它應該是什麼;它顯示已完成的recaptcha,選項: 但是當我點擊測試時,它會回到要求我完成recaptcha。我該如何做到這一點,因此每次(有效)會話只做一次?要求Recaptcha每個會話只有一次

<html> 
    <head> 
     <title>Admin Settings Page</title> 
     <script src='https://www.google.com/recaptcha/api.js'></script> 
    </head> 
    <body> 
     <?php 
     if (!isset($_POST['g-recaptcha-response'])){ 
      ?> 
      <p>Please successfully complete the reCaptcha below to access the page.</p> 
      <form action="" method="post"> 
       <div class="g-recaptcha" data-sitekey="sitekey"></div> 
       <input type="submit" value="Open Admin Tools" /> 
      </form> 
      <?php 
     } else { 
      $captcha; 
      if (isset($_POST['g-recaptcha-response'])){ 
       $captcha=$_POST['g-recaptcha-response']; 
      } 
      if (!isset($captcha)) 
       $captcha = false; 
      if (!$captcha){ 
       echo '<h2>Please check the the captcha form.</h2>'; 
       exit; 
      } 
      $secretKey = "secretkey"; 
      $ip = $_SERVER['REMOTE_ADDR']; 
      $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 
      $responseKeys = json_decode($response,true); 
      if (intval($responseKeys["success"]) !== 1) { 
       echo '<h2>reCAPTCHA expired</h2>'; 
      } else { 
       ?> 
       <p>Recaptcha Completed, Options:</p> 
       <form action="" method="post"> 
        <input type="submit" value="test" /> 
       </form> 
       <?php 
      } 
     } 
     ?> 
     <br /> 
    </body> 
</html> 

我有一個臨時的解決方法,我使用的動態改變通過JavaScript的內容,但我仍想知道是否有可能做什麼,我問上面。

回答

1

您正在測試$_POST驗證碼,但您應該已將驗證碼狀態保存在會話變量中。

你在哪裏測試if (isset($_POST['g-recaptcha-response'])),你可以測試$_SESSION['captcha_valid']。並且,如果顯示的文字說明驗證碼已完成,請使用類似$_SESSION['captcha_valid'] = true的內容。

這樣,只要用戶解決驗證碼,只要會話有效,此新狀態就會保持有效。