kyme Posted February 24, 2009 Share Posted February 24, 2009 Hello, Im newbie in PHP, and till now stil learning php.. I'm sorry but im really a pretty slow in comes of programming. But im trying my best not to discourage and keep it up till i get my goal. I really2x want to learn php & be part of it and starting to love on it, cause i know i can do it with PHP. Now im working a project a which has a spam code before it would submit and email it. But my problem is, It would not display the spam code or captcha.. Heres my code. Html form <form action="page/contact.php" method="post" style="size:inherit" align="center"> <p>Name: </br><input type="text" size="20" name="name" onFocus="doFocus(this,'name'); return false;"/></p> <p>Email:</br><input type="text" size="20" name="email"/></p> <p>Message </p> <textarea width="15" height="40" name="message" style="overflow-x:hidden;" rows="5" cols="20" onFocus="doFocus(this,'message'); return false;" /> </textarea> <br /><br/> Enter code below: <img src="page/secimg.php"> <br/> <input type="text" name="Spam Code" size="7" /> <br/> <input type="submit" name="submit" value="SEND"size="5" name="email"/> </form> And heres my PHP script <?php session_start(); $width = 120; $height = 40; $font = 'monofont.ttf'; $length=5; $possible = '1234567890bcdfghjkmnprstvwxyzABCDEFJKMNPTUVWXY'; $i = 0; while ($i < $length) { $string .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } //$captcha = imagecreatefrompng("./captcha.png"); $captcha = imagecreate($width, $height); $back = imagecolorallocate($captcha, 0, 0, 55); $fore = imagecolorallocate($captcha,233,239,239); //$fore = imagecolorallocate($captcha,255,255,255); //imageline($captcha,40,0,64,29,$line); $n_lines = mt_rand(6, ; // random number of lines for ($i = 0; $i < $n_lines; $i++) { $tm = mt_rand(1, 2); // random types of lines if ($tm == 1) { $x1 = 0; $y1 = mt_rand(0, $height); $x2 = $width; if ($y1 > $height / 2) $y2 = mt_rand(0, $height * 0.3); else $y2 = mt_rand($height * 0.5, $height); } elseif ($tm == 2) { $x1 = mt_rand(0,$width); $y1 = 0; if ($x1 > $width / 2) $x2 = mt_rand(0, $width * 0.3); else $x2 = mt_rand($width / 2,$width); $y2 = $height; } imageline($captcha,$x1,$y1,$x2,$y2,$fore); } $angle = mt_rand(-14,14); // add some angle $fontsize = $height * 0.70; $bounds = imagettfbbox($fontsize, $angle, $font, $string); //var_dump($bounds); list(,$btmleftY,,$btmrightY,$toprightX,$toprightY,,) = $bounds; //$x = ($width - $toprightX)/2; $y = ($height - $toprightY)/2; $x = mt_rand(5, $width - $toprightX - 5); $shootL = $height - $btmleftY; $shootR = $height - $btmrightY; if ($shootL > 0) $y - $shoot; else if ($shootR > 0) $y - $shoot; imagettftext($captcha, $fontsize, $angle, $x, $y, $fore, $font , $string); $_SESSION['key'] = md5($string); /* Output the image */ header("Content-type: image/png"); imagepng($captcha); imagedestroy($captcha); ?> In this part were it all checks the each textbox to verify <?php define("kContactEmail","name@email.com"); // init variables $error_msg = "The following fields were left empty or contain invalid information:<ul class='error'>"; $error = false; function checkEmpty($names, $data) { $n = count($names); $emptys = array(); for ($i = 0; $i < $n; $i++) { if (empty($data[$names[$i]])) $emptys[] = $names[$i]; } return $emptys; } function createUList($arr) { $n = count($arr); echo '<ul>'; for ($i = 0; $i < $n; $i++) echo " <li>".str_replace('_',' ',$arr[$i])."</li>\n"; echo '</ul>'; } // determine is the form was submitted $submit = $_POST['submit']; if (empty($submit)) $form_submitted = false; else $form_submitted = true; if ($form_submitted) { $emptyFlds = checkEmpty($infoToCheck,$_POST); if($emptyFlds){ } else{ // read out data $name = $_POST['name']; $email = $_POST['email']; // $subject = $_POST['subject']; $message = $_POST['message']; $validSecurityCode = md5($_POST['Spam_Code']) == $_SESSION['key']; // verify required data if ($noEmptyFlds && $validSecurityCode) { unset($_SESSION['key']); // prevent multiple send $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; } if(!$name) { $error_msg .= "<li>Full Name</li>"; $error = true; } if(!$email) { $error_msg .= "<li>E-mail Address</li>"; $error = true; } // if(!$subject) { $error_msg .= "<li>Subject</li>"; $error = true; } if(!$message) { $error_msg .= "<li>Message</li>"; $error = true; } if($email) { if(!eregi("^[a-z0-9_]+@[a-z0-9\-]+\.[a-z0-9\-\.]+$", $email)){ $error_msg .= "<li>E-mail Address</li>"; $error = true; }} $error_msg .= "</ul>"; // email message if no errors occurred if (!$error) { // prepare message $msg = "Full Name: \t $name \n"; $msg .= "E-mail Address: \t $email \n"; $msg .= "Message: \n---\n $message \n---\n"; // prepare message header $mailheaders = "MIME-Version: 1.0\r\n"; $mailheaders .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $mailheaders .= "From: $name <$email>\r\n"; $mailheaders .= "Reply-To: $name <$email>\r\n"; // send out email mail(kContactEmail, $subject ,stripslashes($msg), $mailheaders); } } } ?> It would not display the the spam code even i uploaded it at the server. I dont know which part should i add something in order it will display. Did i mess or miss something? Please Help me I just found this at the book and try to implement it and understand the logic code. :'( Quote Link to comment Share on other sites More sharing options...
gevans Posted February 24, 2009 Share Posted February 24, 2009 Do you have GD enabled? have a look bellow CLICKY Quote Link to comment Share on other sites More sharing options...
kyme Posted February 25, 2009 Author Share Posted February 25, 2009 Hello sir, I tried to read it and i dont understand a little bit. What does GD do? And how to work out? My friend just tested this up before i have implemented on it. And it works fine and 100% working. Now i have tried the codes but in my page doesnt work and never it displayed Could anyone help me that more understandable and i can easly catch up. Thanks.. Quote Link to comment Share on other sites More sharing options...
DarkSuperHero Posted February 25, 2009 Share Posted February 25, 2009 GD takes care of image processing, so any images, like captchas require this image processing so they can be displayed. PHP uses the library to create the images...try uploading a php file, and using phpinfo(); function.... gd should be listed....and it will tell you if its on or off... Quote Link to comment Share on other sites More sharing options...
kyme Posted February 25, 2009 Author Share Posted February 25, 2009 Now it works, it displays now an spam code but the problem now is the How it test if it is correct... Heres the code , In this part it wont test if the spam box was correct filling on it. <?php define("kContactEmail","name@email.com"); // init variables $error_msg = "The following fields were left empty or contain invalid information:<ul class='error'>"; $error = false; function checkEmpty($names, $data) { $n = count($names); $emptys = array(); for ($i = 0; $i < $n; $i++) { if (empty($data[$names[$i]])) $emptys[] = $names[$i]; } return $emptys; } function createUList($arr) { $n = count($arr); echo '<ul>'; for ($i = 0; $i < $n; $i++) echo " <li>".str_replace('_',' ',$arr[$i])."</li>\n"; echo '</ul>'; } // determine is the form was submitted $submit = $_POST['submit']; if (empty($submit)) $form_submitted = false; else $form_submitted = true; if ($form_submitted) { $emptyFlds = checkEmpty($infoToCheck,$_POST); if($emptyFlds){ } else{ // read out data $name = $_POST['name']; $email = $_POST['email']; // $subject = $_POST['subject']; $message = $_POST['message']; $validSecurityCode = md5($_POST['Spam_Code']) == $_SESSION['key']; // verify required data if ($noEmptyFlds && $validSecurityCode) { unset($_SESSION['key']); // prevent multiple send $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; } if(!$name) { $error_msg .= "<li>Full Name</li>"; $error = true; } if(!$email) { $error_msg .= "<li>E-mail Address</li>"; $error = true; } // if(!$subject) { $error_msg .= "<li>Subject</li>"; $error = true; } if(!$message) { $error_msg .= "<li>Message</li>"; $error = true; } if($email) { if(!eregi("^[a-z0-9_]+@[a-z0-9\-]+\.[a-z0-9\-\.]+$", $email)){ $error_msg .= "<li>E-mail Address</li>"; $error = true; }} $error_msg .= "</ul>"; // email message if no errors occurred if (!$error) { // prepare message $msg = "Full Name: \t $name \n"; $msg .= "E-mail Address: \t $email \n"; $msg .= "Message: \n---\n $message \n---\n"; // prepare message header $mailheaders = "MIME-Version: 1.0\r\n"; $mailheaders .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $mailheaders .= "From: $name <$email>\r\n"; $mailheaders .= "Reply-To: $name <$email>\r\n"; // send out email mail(kContactEmail, $subject ,stripslashes($msg), $mailheaders); } } } ?> Quote Link to comment Share on other sites More sharing options...
kyme Posted February 26, 2009 Author Share Posted February 26, 2009 stil wont work, Please help Quote Link to comment Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 $validSecurityCode = md5($_POST['Spam_Code']) == $_SESSION['key']; echo $validSecurityCode; print security code on submitted page and check if it is correctly displayed. Quote Link to comment Share on other sites More sharing options...
kyme Posted March 2, 2009 Author Share Posted March 2, 2009 still confusing Quote Link to comment Share on other sites More sharing options...
kyme Posted March 4, 2009 Author Share Posted March 4, 2009 if(!$name) { $error_msg .= "<li>Full Name</li>"; $error = true; } if(!$email) { $error_msg .= "<li>E-mail Address</li>"; $error = true; } // if(!$subject) { $error_msg .= "<li>Subject</li>"; $error = true; } if(!$message) { $error_msg .= "<li>Message</li>"; $error = true; } if($email) { if(!eregi("^[a-z0-9_]+@[a-z0-9\-]+\.[a-z0-9\-\.]+$", $email)){ $error_msg .= "<li>E-mail Address</li>"; $error = true; }} $error_msg .= "</ul>"; I think i have problem in this part, i have something miss it? Still no solution.. Quote Link to comment Share on other sites More sharing options...
kyme Posted March 4, 2009 Author Share Posted March 4, 2009 Actually it wont test once i fill w/ the spam code.. Quote Link to comment Share on other sites More sharing options...
stelthius Posted March 4, 2009 Share Posted March 4, 2009 you will know if captcha is working as it will tell you wrong security code, if you know the code is correct id suggest looking at the rest of your script i have a catpcha script i wrote up and use it on a register form, email us form, and a bulletin blog ill show you a example let me dig out my code and ill post it up it may or may not help you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.