Goose87 Posted July 26, 2007 Share Posted July 26, 2007 Hey guys, I was wondering if you could give me some pointers as to creating random number generators for validation. I have tried the obvious rand(1,50) method and display a few numbers in a row, like 241295 and they have to type it. Problem with that is a macro could easily copy and paste that into the text-box, and the other problem is that i don't know how to get the php to check that the code is the same as the random number! I know it prob seems like a simple problem, but its baffled me! Thanks, James. Link to comment https://forums.phpfreaks.com/topic/61943-solved-validation-code-generators/ Share on other sites More sharing options...
Fadion Posted July 26, 2007 Share Posted July 26, 2007 You can use captcha images, they are better looking and very difficult to be caught from scripts then plain text validation. Some time ago i wrote this simple code to generate captcha images. <?php session_start(); $width = 100; $height = 30; $nrChars = 5; $chars = '123456789'; for($i=0; $i<$nrChars; $i++){ $randomChar = rand(0, strlen($chars)-1); $text .= $chars[$randomChar]; } $_SESSION['captcha'] = sha1($text); $image = imagecreate($width, $height); $background = imagecolorallocate($image, 0, 50, 150); $gridColor = imagecolorallocate($image, 0, 0, 250); for($i=0; $i<10; $i++){ imageline($image, 10*$i, 0, 10*$i, $height, $gridColor); } for($i=0; $i<3; $i++){ imageline($image, 0, 10*$i, $width, 10*$i, $gridColor); } for($i=0; $i<50; $i++){ $linesColor = imagecolorallocate($image, $i*4, 0, $i*2); $x1 = rand(0, $width); $x2 = rand(0, $width); $y1 = rand(0, $height); $y1 = rand(0, $height); imageline($image, $x1, $y1, $x2, $y2, $linesColor); } $textColor = imagecolorallocate($image, 255, 255, 255); imagestring($image, 10, 30, 7, $text, $textColor); imagepng($image); ?> If u want to use it, save it in a file (ex. captcha.php) and call it as the src of an image. To compare the text of the captcha with the input text, use the $_SESSION['captcha']. Hope it helps. Cheers. Link to comment https://forums.phpfreaks.com/topic/61943-solved-validation-code-generators/#findComment-308444 Share on other sites More sharing options...
Goose87 Posted July 26, 2007 Author Share Posted July 26, 2007 Absolutely brilliant. Thank you soooo much for your help! I owe you one! Link to comment https://forums.phpfreaks.com/topic/61943-solved-validation-code-generators/#findComment-308458 Share on other sites More sharing options...
Fadion Posted July 27, 2007 Share Posted July 27, 2007 Glad it worked for you Link to comment https://forums.phpfreaks.com/topic/61943-solved-validation-code-generators/#findComment-308462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.