Heres a simple php capcha script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Heres a simple php capcha script

    In the form that you want to submit ad the following code
    Code:
    <?php
        $min_number = 1;
        $max_number = 10;
    
        $random_number1 = mt_rand($min_number, $max_number);
        $random_number2 = mt_rand($min_number, $max_number);
        echo "<span style=\"background-color:#000000; color:#FFFFFF; border:#000099 thin solid\"><b><big>$random_number1 + $random_number2</big></b></span><br />";
    
    ?>
    <input name="result" type="text" size="3" /><br />
    <input name="n1" type="hidden" value="<?=$random_number1; ?>" />
    <input name="n2" type="hidden" value="<?=$random_number2; ?>" />
    Ok, in the submit page ad the following code
    Code:
    <?php
        $result = $_POST["result"];
        $n1 = $_POST["n1"];
        $n2 = $_POST["n2"];
        $total = $n1 + $n2;
    
    if ($result == $total) {
        // Paste content here, which will show if the code is entered correct. I left the javascript in just to make it easier for you to understand for first use.
    echo "<script>alert('Correct answer');</script>";
    } else {
        // If the code is wrong it will outpt this...
    echo "<script>alert('Wrong answer entered, please retry');</script>";
        
    }
    ?>
    This is just an example, but it can be more modified, but this is a simple way of having a capcha system on your website.
    I will be posting another code too soon which uses images instead of text.

  • #2
    bots or just simple grabbers will be able to read it

    Comment


    • #3
      Originally posted by GumSlone View Post
      bots or just simple grabbers will be able to read it

      Lol you are right....

      That's why i did this one.
      Insert or edit this to the form of the page.

      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Untitled Document</title>
      </head>
      
      <body>
      <div align="center">
      <div align="center" style="color:#FF0000; border:#FF0000 thin solid; width:15%">
      <form action="2.php" method="post">
      <?php
          $random_number = rand('1','9999');    
          echo "<img src=\"img.php?text=$random_number\" alt=\"$random_number\"  /><br />";
      ?>
      <b>Please enter the verification code as above</b><br />
      <input name="result" type="text" size="6" /><br />
      <input name="random_number" type="hidden" value="<?=$random_number; ?>" />
      <input type="submit" value="Submit" />
      </form>
      </div>
      </div>
      </body>
      </html>
      that should go on the page where you $_POST
      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Untitled Document</title>
      </head>
      
      <body>
      <?php
          $result = $_POST["result"];
          $random_number = $_POST["random_number"];
      
      if ($result == $random_number) {
          // captcha is OK
      echo "<script>alert('Correct answer');</script>";
      } else {
          // wrong captcha
      echo "<script>alert('Wrong answer entered, please retry');</script>";
          
      }
      ?>
      </body>
      </html>
      And this is my img.php file
      Code:
      <?php
      header('Content-Type: image/png');
      $im = imagecreatetruecolor(70, 30);
      $white = imagecolorallocate($im, 255, 255, 255);
      $grey = imagecolorallocate($im, 128, 128, 128);
      $black = imagecolorallocate($im, 0, 0, 0);
      imagefilledrectangle($im, 0, 0, 399, 29, $white);
      $text = $_GET['text'];
      $font = 'georgia.ttf';
      imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
      imagepng($im);
      imagedestroy($im);
      ?>
      You think this will work better?

      Comment


      • #4
        still not safe since grabbers can read the text value from image tag,

        you should create some own encoding function which will encode the text value for image tag, and decode it back to normal in the captcha check page.

        Comment


        • #5
          mmm i think i've got a way, but i will also use this little coding as well, because bots use source instead of html...

          Code:
          <form action="4.php" method="post">
          <label>Test</label><br />
          <input type="text" name="test" />
          <div style="visibility: hidden">
          <label>I'm a human</label>
          <input type="checkbox" name="human" value="robit" />
          </div>
          <input type="submit" value="Submit" />
          </form>
          check page
          Code:
          <?php
          $test=$_POST['test'];
          if (empty($_POST['human']))
          {
          echo "You are a human";
          }
          else
          {
          echo "You are a bot";
          }
          ?>
          Suppose i can ad a few fields and try and trick the bots this way as well?
          It's a simple code, but any defence should be better than none?

          Comment


          • #6
            just create your own encoding and decoding function, its simple, i.e. replace 0 with a, b with 2 and convert it back on the captcha check page

            Comment


            • #7
              nice idea, thanks i will do it in the morning and post back here.

              Comment

              Working...
              X