Jump to content

PHP need help contact form - add captcha


Lykos22
 Share

Recommended Posts

Hi I would like some help please.I 've created a contact form and i would like to add some captcha. I have created some gif images, that have numbers with some noise added on, and named themcode_01.gif for no1,code_02.gif for no2etc etc.Firstly I have put some of them on my form, staticly, for display purposes like this: <label for="captcha">Please type the code you see (*):<br /><img src="images/code_01.gif" width="18" height="30" /><img src="images/code_07.gif" width="18" height="30" /><img src="images/code_01.gif" width="18" height="30" /><img src="images/code_08.gif" width="18" height="30" /><img src="images/code_03.gif" width="18" height="30" /><img src="images/code_07.gif" width="18" height="30" /><input name="captcha" type="text" id="captcha" /></label><span id="spryCaptcha"> <input type="text" name="captcha" id="captcha" tabindex="50" /><span class="textfieldRequiredMsg">You have to submit this field.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span><br /> So in order to put some functionality in and show randomly 6 digit images i made this and work fine: <label for="captcha">Please type the code you see (*):<br /> <?php for($i=0; $i<=5; $i++){ ?> <img src="_assets/captcha/code-0<?php echo rand(0,9); ?>.gif" width="18" height="30" /> <?php } ?></label> Everytime I refresh the page, 6 new numbers generate. My problem is how to check that the combination of the random images will match with the combination of numbers that user enters in the input field in order to work captcha correct??Any help would be appreciated.

Link to comment
Share on other sites

you have to keep track of the numbers you randomly generating and store it in session and in the form you need to check against the session if it is matching or not. CAPCTAHCA usualy dont work like this. it generates some alphanumeric characters and makes the image dynamically using http://php.net/gd2 GD image library

Edited by birbal
Link to comment
Share on other sites

you have to keep track of the numbers you randomly generating and store it in session and in the form you need to check against the session if it is matching or not. CAPCTAHCA usualy dont work like this. it generates some alphanumeric numbers and makes the image dynamically using http://php.net/gd2 GD image library
I have already tried tthe GD image library but didn't display the image, although I have gd library installed
Link to comment
Share on other sites

if you want to fix it ,you can post that code of gd, with errors showing if any is there so that we can help you to get it working.

Link to comment
Share on other sites

Ok so in contact.php i have: <?php require_once("_core/init.php"); ?> /// in init.php i have already session_start();<?php//session_start();$_SESSION['captcha'] = rand(100000,999999);if(empty($_POST) === false){ //grab the inputs $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $message = trim($_POST['message']); $captcha = trim($_POST['captcha']); ........ <label for="captcha">Please type the code you see (*):<br /> <img src="_includes/captcha.php" /></label><span id="spryCaptcha"> <input type="text" name="captcha" id="captcha" tabindex="50" /><span class="textfieldRequiredMsg">You have to submit this field.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span><br /> <p> </p> but shows no image. And inside _includes folder in captcha.php : <?phpsession_start();header("Content-type: image/jpeg");$text = $_SESSION['captcha'];$font_size = 20;$image_width = 200;$image_height = 40;$image = imagecreate($image_width, $image_height);imagecolorallocate($image, 255,255,255);//rgb white$text_color = imagecolorallocate($image, 0,0,0);for($i=1; $i<=30;$i++){ $x1 = rand(1,100); $x2 = rand(1,100); $y1 = rand(1,100); $y2 = rand(1,100); imageline($image, $x1, $y1, $x2, $y2, $text_color);}imagettftext($image, $font_size, 0, 15, 30, $text_color, 'georgia.ttf', $text);imagejpeg($image);?> Some added feedback:If i remove comments from the 2nd session_start() it shows a broke image icon and also displays a warning that already has started a session (because of the first one in init.php)I also tried to make it with imagestring() function but didn't work.

Link to comment
Share on other sites

you dont need to start session using session_start() in every page. using it in the top of the included page will work fine.when that session satrted error is showing because it is making an output to the browser. once a output started you cant send another header so that image header is not working. that is the reason for broken image.

Edited by birbal
Link to comment
Share on other sites

you dont need to start session using session_start() in every page. using it in the top of the included page will work fine.when that session satrted error is showing because it is making an output to the browser. once a output started you cant send another header so that image header is not working. that is the reason for broken image.
Exactlly! that's why I ve put session_start in here <?php require_once("_core/init.php"); ?> and put comments to the next one. But then it doesn't show any image at all
Link to comment
Share on other sites

first just run the captcha.php directly from the browser. what does it show to you?

Link to comment
Share on other sites

first just run the captcha.php directly from the browser. what does it show to you?
FF shows a message 'Cannot prview image "localhost/mysite/_includes/captcha.php" because having errors'and if I view source the contact.php shows <img src="_includes/captcha.php" />
Link to comment
Share on other sites

remove the image header captcha.php page. what does it show? does it show you any error? and also make sure no output has been made before image output even a blank space.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...