0

I want to create a simple captcha system with php . But I'm having some problems:

<?php
    // init variables
    $min_number = 15455551;
    $max_number = 94577758;

    // generating random numbers
    $random_number1 = mt_rand($min_number, $max_number);
    $random_number2 = mt_rand($min_number, $max_number);
?>
    <form method="POST">
        <p>
            Resolve the simple captcha below: <br />
            <?php
                echo $random_number1 . ' + ' . $random_number2 . ' = ';
            ?>
            <input name="captchaResult" type="text" size="2" />

            <input name="firstNumber" type="hidden" value="<?php echo  $random_number1; ?>" />
            <input name="secondNumber" type="hidden" value="<?php echo $random_number2; ?>" />
        </p>

        <p>
            <input type="submit" value="submit" />
        </p>
    </form>
<?php
        $captchaResult = $_POST["captchaResult"];
        $firstNumber = $_POST["firstNumber"];
        $secondNumber = $_POST["secondNumber"];

        $checkTotal = $firstNumber + $secondNumber;

        if ($captchaResult == $checkTotal) {
            echo '<input id="btn" type="submit" value="Get" />';
        } else {
            echo '<h2 class="red">Wrong Captcha. Try Again</h2>';
        }
    ?>

It echo the 'Get' button without submitting captcha . It's the code problem . And I want to create a

<a href="code.php">Get Code</a>

button, user need go this page and then they get a code for it. I delete the code from index.php

        Resolve the simple captcha below: <br />
        <?php
            echo $random_number1 . ' + ' . $random_number2 . ' = ';
        ?>

And add a code on code.php something like this

<?php
     $result= $random_number1 + $random_number2;
 echo "$result";
 ?>

but how can I call the code which is already generated in index.php by user .

Utpal Sarkar
  • 412
  • 5
  • 23
  • The way it is written your code will always be executed. Try putting an if statement...like "if (isset($_POST['captchaResult']))" so it only executes IF you have posted the info. – ckimbrell Sep 26 '16 at 17:46
  • Thats not how captcha works, a quick google search may be a good idea. If you sent the data needed to work out your captcha to the browser, then we can all write code to avoid it – RiggsFolly Sep 26 '16 at 17:52

0 Answers0