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
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
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
Yes, it would. The else always triggers - you would need to rewrite the else into an elseif or something