Image Creation on the lfy with some number in it

Posted on 2004-10-29
Last Modified: 2006-11-17
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
Question by:AJAY CHADHA
    LVL 27

    Expert Comment

    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
    LVL 27

    Expert Comment

    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:

    You could also make use of the rand function ( to randomly position the line each time.

    Full reference of the image functions:
    LVL 2

    Expert Comment

    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);
    LVL 8

    Author Comment

    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?
    LVL 27

    Accepted Solution

    The GD site is

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

    Go to 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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now