The list of all PHP and Mysql posts

How to make a verification text field

You must have seen verification fields , users have to enter a text manually to submit their information in to the website. This verification prevents hackers to make a program to enter a data too many time and fill your server with nonsense things.
for example the figure here shows a verification field for a sign up page.



The verification text is a random generated code that has been converted in to image.
Put this code in the page that you want to show verification code in it. (i.e. sign up page).

code:

<?php
create_code(); // a function to create a random number to show in verification area
?>
<BR>
<img src="Security_Image.php">
<BR>

Now in a file that you put your functions (and you can include it to the begining of any other php file) add these codes for create_code function.


code:

function create_code()
{
session_start();
$SecCode = md5(rand(0,9999)); // create random number and "md5" function converts it in to a hashed code
$SecCode = strtolower($SecCode); // change all of the letters to lower case
$_SESSION['SecImageStr'] = strtoupper(substr($SecCode,0,4)); // change it to a four letters code
}


Now make a php file with "Security_Image.php" name. and put these codes in it.


code:

session_start();
$Str = $_SESSION['SecImageStr'];
$SecImage = imagecreate( 3*50, 3*20);
$BgColor = imagecolorallocate( $SecImage, 255, 150, 220); // Background color
$FrColor = imagecolorallocate( $SecImage, 0, 0, 255); // ForeGround color
$LineColor = imagecolorallocate( $SecImage, 255, 0, 150); // Lines color
for($Index = 0; $Index != 6; $Index++)
{
$LineDegree = rand(15, 50);
imageline ( $SecImage, $Index, $LineDegree, 2*($Index+1) * 20, 2*$Index,$LineColor ); // draw lines
}
imagestring ( $SecImage, 40, 10*5, 10*1, $Str, $FrColor ) ; // convert code text in to image and save it in to $SecImage
imagejpeg ( $SecImage , '', 100 ) ; // make a jpg image from $SecImage
header("Content-type: image/jpg"); // show the image on the screen in users' browser
imagedestroy ( $SecImage ) ; // there's no more need to the image so it is deleted

Now , you can have  a verification code field.

1 comment: