Jump to content

[SOLVED] validation code generators


Goose87

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.