Cory94bailly Posted May 27, 2008 Share Posted May 27, 2008 Well I have an old captcha script that a friend made for me.. It worked on my old free host but for some reason, it's not working now... Captcha.php: <? session_start(); function random_number($length=3) { $chars = "123456789abcdefghijklmnpqrstuvwxyz"; $code = ""; while (strlen($code) < $length) { $code .= $chars[mt_rand(0,strlen($chars))]; } return $code; } $im = imagecreate(26, 15); $string = random_number(); $_SESSION['string'] = $string; $bg = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); for($i=0;$i<3;$i++){ $y = 20; imagestring($im, 5, 0, 0, $string, $black); $x = $x + 10; } header('Content-type: image/gif'); imagegif($im); ?> Form.php: <html> <head> <title>Test with Captchas</title> </head> <body> Note: All 0's are zeros, there are no letter o's. <form action="test.php" method="POST"> <table border="0"> <tr><td><img src="captcha.php"></td></tr> <tr><td>Captcha:</td><td><input type="text" name="captcha"><br></td></tr> </table> <input type="submit" name="enter" value="enter"> </form> </body> </html> Test.php: <? session_start(); //if the string in the image exist if (isset($_SESSION['string'])) { //if the captcha you write and the captcha in the image are the same if ($_POST['captcha'] == $_SESSION['string']) { //show "correct" echo 'Correct!'; //if not } else { //show incorrect echo 'Incorrect!'; //if the string in the image dont exist }} else { //shows "error" echo 'Error!!!'; } session_end(); ?> Also, would there be any other better way to do this? Like how do I make the letters harder to see and stuff? Quote Link to comment Share on other sites More sharing options...
trq Posted May 27, 2008 Share Posted May 27, 2008 Seriously, just stating that something doesn't work is useless to us. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted May 27, 2008 Author Share Posted May 27, 2008 Seriously, just stating that something doesn't work is useless to us. Ok umm I press submit on form.php and get a blank page. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted May 27, 2008 Author Share Posted May 27, 2008 Never mind, I found a new script.. <?php session_start(); class CaptchaSecurityImages { var $font = 'monofont.ttf'; function generateCode($characters) { $possible = '23456789bcdfghjkmnpqrstvwxyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; } function CaptchaSecurityImages($width='120',$height='40',$characters='6') { $code = $this->generateCode($characters); //Font size will be 75% of the image height $font_size = $height * 0.75; $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); //Set the colours $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); //Generate random dots in background for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } //Generate random lines in background for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } //Create textbox and add text $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function'); //Output captcha image to browser header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; } } $width = isset($_GET['width']) && $_GET['height'] < 600 ? $_GET['width'] : '120'; $height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40'; $characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6'; $captcha = new CaptchaSecurityImages($width,$height,$characters); ?> 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.