Can someone help me. I'm almost positive that this worked at one point, but I can't for my life get it to work. I have a page with a short form that uses an image verification. Thanks to the help of a member here for the script. My problem is I need to pass the value of the image in my captcha file to my submit form for validation. It validates whether the user typed in the right code for the image. Simple. For some reason, the Session returns NULL. I'm posting the code below. These are the lines for the form tag and then the actual input field: Page1.php <FORM METHOD=post name=formK action="submit_form.php"> <input class="formTextBox" name="vercode2" type="text" maxlength="4" style="width:85px;" /> Code (markup): Here is my captcha code: captcha.php <?php session_start(); $_SESSION["vercode2"] = $vercode_s; $length = 4; $vercode_s = ""; while ($length > 0) { $vercode_s .= strtoupper(dechex(mt_rand(0,15))); $length -= 1; } header('Content-type: image/jpeg'); $image = imagecreatetruecolor(125,45); imageantialias($image,false); $black = imagecolorallocate($image,0,0,0); $white = imagecolorallocate($image,255,255,255); imagefill($image,1,1,$white); for($x=0;$x<145;$x+=rand(8,8)) imageline($image,$x,0,$x,45,$black); for($y=0;$y<40;$y+=rand(8,8)) imageline($image,0,$y,125,$y,$black); for($i=0,$j=12;$i<4;++$i,$j+=26) imagettftext($image,36,0,$j,38,$black,'courier_bold.ttf',$vercode_s[$i]); imagejpeg($image); ?> Code (markup): And finally my submit form code: submit_form.php <?php session_start(); // Report all PHP errors error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); echo session_id(); $continue = 1; $vercode_p = $_POST['vercode2']; $vercode_s = $_SESSION['vercode2']; $email = $_POST['email']; $errormsg = ""; //Initialize errors if ($email == "" OR $email == "Email Address") { //If the email field is not entered echo "Missing Email"; $errormsg = "Missing Email"; $continue = 0; } if ($vercode_p == "" OR $vercode_p == "Code") { echo "Code is not entered."; if ($vercode_p != $vercode_s) { //If the code from the from field does not match the code stored in the session. echo "Code mismatch."; if ($errormsg) { // If there is already an error, add the next error to it. $errormsg = $errormsg . " & Missing or Incorrect Verification Code"; $continue = 0; } } } if ($errormsg) { //If any errors exist, display them. echo $errormsg . ". Please press the back button to try again."; } //If all fields present and correct if ($continue == 1) { //Do something echo "SUCCESS!"; //echo $errormsg; } echo var_dump ($vercode_s); if(isset($_SESSION['vercode2']) && !empty($_SESSION['vercode2'])) { echo 'Set and not empty, and no undefined index error!'; } else { echo 'Is not set or is empty'; } ?> <html> <body> <p>v_email: <?php echo $email?></p> <!----Check to make sure $_POST email saved to variable---> <p>v_code: <?php echo $vercode_p?></p> <!----Check to make sure $_POST vercode2 saved to variable----> <p>v_sessn: <?php echo $vercode_s?></p> <!----check to make sure $_SESSION vercode2 saved to variable----> <p>p_email: <?php echo $_POST['email'] ?> <!---What was put in the email text field----> <p>p_code: <?php echo $_POST['vercode2'] ?><!----What was put in the verification code text field----> <p><a href="page1.php">Click Here to Return</a> <!----Go back to the form page----> </body> </html> Code (markup): Any help would be greatly appreciated!
In captcha.php you are assigning $vercode_s to the session storage and in form_submit.php you are assigning the session variable to $vercode_s. But, you never actually assign any value to it. I've moved one line in your captcha.php file which should work. <?php session_start(); $length = 4; //Start with an empty string $vercode_s = ""; //Build the string while ($length > 0) { $vercode_s .= strtoupper(dechex(mt_rand(0,15))); $length -= 1; } //Store the string in the session $_SESSION['vercode2'] = $vercode_s; header('Content-type: image/jpeg'); $image = imagecreatetruecolor(125,45); imageantialias($image,false); $black = imagecolorallocate($image,0,0,0); $white = imagecolorallocate($image,255,255,255); imagefill($image,1,1,$white); for($x=0;$x<145;$x+=rand(8,8)) imageline($image,$x,0,$x,45,$black); for($y=0;$y<40;$y+=rand(8,8)) imageline($image,0,$y,125,$y,$black); for($i=0,$j=12;$i<4;++$i,$j+=26) imagettftext($image,36,0,$j,38,$black,'courier_bold.ttf',$vercode_s[$i]); imagejpeg($image); ?> PHP: Also, you might want to clean up your form code a bit. <form method="post" name="formK" action="submit_form.php"> Code (markup):
THANK YOU ekm941!! I don't think I would have ever picked that up! I'm a beginner, as you can probably tell. This works perfectly!