problem with form continious loop

Discussion in 'PHP' started by saturn100, Aug 27, 2014.

  1. #1
    Hi
    I am trying to create a login form for some one
    I have doe this before but this time the client want all error messages to go back to the form like this example http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_required

    He also wants a captha and this I am having issues with

    I am only working on the captha now using a basic random variable but each time the page is posted it just says wrong captha

    Its is a continual loop

    my main page code is

    
    <?php
    
    
    $no ="";
    $yes ="";
    
    ?>
    <?php
    include 'captha.php';
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if ($_POST["captcha"]!=""&&$code==$_POST["captcha"]) {
       
       $yes ="OK";
    }
       else {
         
       $no ="Wrong Captha";
       
       
    }
    }
    
    ?>
    
    <html>
    
    <body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    Enter Image Text
    <input name="captcha" type="text">
    <?php echo $code; ?><br>
    <input name="submit" type="submit" value="Submit">
    </form>
    <br>
    <br>
    <?php echo $no;?>
    <?php echo $yes;?>
    </body>
    
    </html>
    
    Code (markup):
    and my captha page is

    <?php

    $code=rand(1000,9999);


    ?>

    I am having other issues turning it into an image but if I can fix the loop issues first I'd be happy
     
    saturn100, Aug 27, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    The code you posted will never, ever work. Simply because when you submit the form, the captcha rerolls, and what the user type in the form will never ever match (unless you're insanely lucky and the rand() rolls the same two times in a row - I wouldn't count on it.
    
    <?php
    
    $captchareturn = '';
    
    require_once('captcha.php');
    
    $enteredvalue = (isset($_POST['captcha'])) ? $_POST['captcha'] : '';
    $captchaverification = (!empty($_POST['captchaverification'])) ? $_POST['captchaverification'] : '';
    
    if (!empty($enteredvalue) && $captchaverification != $enteredvalue) {
       $captchareturn = 'Wrong Captcha';
    }
    
    echo '
       <!DOCTYPE html>
         <html>
           <head>
             <title>Captcha test</title>
           </head>
         <body>
           <form method="post">
             <label for="captcha">Enter image text:</label>
             <input type="hidden" name="captchaverification" value="'.$code.'">
             <input name="captcha" id="captcha" type="text" value="'.$enteredvalue.'">
             '.$code.'
             <input name="submit" type="submit" value="Submit">
           </form>
           '.$captchareturn.'
         </body>
     </html>';
    
    ?>
    
    Code (markup):
    cleaned up, and working
     
    PoPSiCLe, Aug 27, 2014 IP
  3. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #3
    OMG
    Thank you for that its been bugging me all day
    I knew what I was doing wrong but not how to fix it

    Just one other thing
    If I wanted to redirect after a successful imput how do I do it
    I tried
    if (!empty($enteredvalue) && $captchaverification != $enteredvalue) {
    $captchareturn = 'Wrong Captcha';
    }
    else {
    header('Location: http://www.example.com/');
    }

    but it seems to redirect me on load now

    Thanks again
     
    saturn100, Aug 27, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Yes, it would. The else always triggers - you would need to rewrite the else into an elseif or something
     
    PoPSiCLe, Aug 29, 2014 IP