ryoakg
1/19/2017 - 4:33 AM

PHPでCAPTCHA実装

PHPでCAPTCHA実装

<?php
if (PHP_SAPI === 'cli') {
  echo 'Usage:' . PHP_EOL;
  echo '  Download Securimage archive from http://www.phpcaptcha.org/download/ and decompress.' . PHP_EOL;
  echo '  run: php -S localhost:8080' . PHP_EOL;
  echo "  browse: http://localhost:8080/{$argv[0]}" . PHP_EOL;
  exit;
}

session_start();

if(isset($_POST['captcha_code'])){
  require_once("./securimage/securimage.php");
  $securimage     = new Securimage();
  if ($securimage->check($_POST['captcha_code']) == false) {
    echo "認証失敗!";
    exit;
  } else {
    header('Location: http://www.google.co.jp') ;
  }
}
?>
<html>
  <head>
    <title>secureimage catpcha test</title>
    <script>
     function alter_image(){
       document.getElementById('captcha').src = './securimage/securimage_show.php?' + Math.random();
     }
    </script>
  </head>
  <body>
    <div>
      <form action="" method="post">
        <img id="captcha" src="./securimage/securimage_show.php" alt="CAPTCHA Image" /><br />
        <input type="text" name="captcha_code" size="10" maxlength="6" />
        <a href="#" onclick="alter_image()">
          <img src="./securimage/images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/>
        </a>
        <input type="submit" value="check">
      </form>
  </body>
</html>