• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Image Creation on the lfy with some number in it

Hello Fellow Coders,

I would like to ask if someone has already created a php script that is used on most of the registration and sign up forms these days where the applicatant is asked to enter the numbers as shown in the picture on the side to prevent the misuse of the registration system.

I hope I have been able to describe exactly what I am looking for. Please feel free to share your PHP codes :)

Have a Good Day :)

Ajay Chadha
  • 3
1 Solution
Hi Ajay,

The basic process to do this is to first create a random string to use for the verification and then use the GD Library (which you will be required to have installed on your server) to create the image and display it. You can then cross reference the string entered by the user with the string generated and check that they match.

There are a few good examples of the process on the web, "php image verification" is a good search term. One example of which is:


That code would go in a file called something.php and would be loaded into your form using <img src="something.php">

You might want to put the string generated in a session so it can be referenced later so you would need session_start(); at the top of your file, then to save changing the code too much in the example link you would replace:

$string = substr(md5(time()),$start,$characters);


$_SESSION['veri'] = substr(md5(time()),$start,$characters);
$string = $_SESSION['veri'];

Then once they have entered the number and posted the form you can check that it matches the session value, adding session_start(); at the top of the file again to initialize the session, then for example:

if ($_POST['your_inputs_name'] == $_SESSION['veri']) {
 // they have entered the right string
Also, in this example its just a basic output of the characters, some verification images also include lines across the text etc. You could recreate similar things using the available functions eg: http://www.php.net/manual/en/function.imageline.php

You could also make use of the rand function (http://us2.php.net/manual/en/function.rand.php) to randomly position the line each time.

Full reference of the image functions:

Try this,  and refresh page you observe different numbers

header ("Content-type: image/png");
$im = @imagecreate (200, 200) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5, implode(" ",preg_split("//",(rand(200,36000)))) , $text_color);
imagepng ($im);
imagedestroy ($im);
AJAY CHADHAAuthor Commented:
Where to get the GD library from?

We are using Windows Express Installer version of the PHP and GD library is not installed with that.

so, from where we can get the library, what files are required to be installed and where?
The GD site is http://www.boutell.com/gd/

But apparently the dll you need they are supplying is not currently for php so you could do this...

Go to php.net and download the windows zip package and grab the gd dll out of it
(for the php 5 package its in the folder called ext and is named php_gd2.dll)

You then need to copy that dll into your extensions folder, on Windows i think it is the Windows directory, check what it is specified as in the php.ini file. You also need to uncomment the GD extension in the ini file, this line:


remove the ;

You will then need to restart your web server.

To check its installed correctly create a test file containing <?php phpinfo(); ?> and access it and look for the section about gd, you should see various settings all flagged as enabled.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now