Link to home
Start Free TrialLog in
Avatar of AJAY CHADHA
AJAY CHADHAFlag for India

asked on

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
Avatar of Diablo84
Diablo84

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:

http://portfolio.meforme.net/samplecode/php/imageverification/

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

with:

$_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:

http://www.php.net/manual/en/ref.image.php
Try this,  and refresh page you observe different numbers

<?php
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);
?>
Avatar of AJAY CHADHA

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial