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. Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 27, 2007 Share Posted July 27, 2007 Glad it worked for 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.