IMAGE VERIFICATION WITH PHP

Image verification has become an integral part in any online registration process. It is important to know whether the other side that is filling the form is really a human or any robot or automatic scripts so, its time to look at how it really works.


To achieve this with PHP, you need to have the GD library installed. The whole procedure is quite simple. You generate a random number and then stamp it over a picture and there you go, you have your own verification number on a nice picture.


The coding is also quite simple as the algorithm mentioned above. Enough of words and let’s take a look at the coding to make the things go clear.



//verification.php



$alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";



// generate the5 digit verification code


$rand = substr(str_shuffle($alphanum), 0, 5);



//create image from a given jpeg to write our code


$image = imagecreatefromjpeg("background.jpg");



//allocate the text color of the code


$textColor = imagecolorallocate($image, 0, 0, 0);



// write the code on the background image


imagestring ($image, 5, 5, 8, $rand, $textColor);



// send the content type header so the image is //displayed properly


header(‘Content-type: image/jpeg’);



// send the image to the browser


imagejpeg($image);



// destroy the image to free up the memory


imagedestroy($image);


?>


Save the above code as verification.php and preview it in your browser and there you have it.



How it works


In the above program, we created a string of alphanumeric characters. Then the sting was shuffled using str_shuffle(string) function. We then extracted the first five characters of the shuffled string using the substr(string,starting_position,no_of_chars). The extracted characters were then stored as our random code. We then created a image from the given image and wrote our code on the image using the imagestring(image,font,x_pos,y_pos,string,string_color). (Refer the imagettftext() function for better result).


And finally the image was fed to the browser using the imagejpeg(image) function for jpeg image. For a complete reference on the functions used in above codes, you can log on to the official site of php,


http://www.php.net/ and get your copy of php manual for free.



Now you have your verification code, but what about using it to verify the users. They say life is full of options and so does php offers a variety of options to pass a variable between the web pages. You can pass the random code generated to any other page either using sessions or cookies variables, using the form submission method or the rather insecure URL method. Let’s try it using session variables. For this you need to have some modifications on your verification.php file or whatever you named it. first of all initialize a session and then assign the random number to a session variable.


//mod_verification.php



Session_start();



$alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";



// generate the5 digit verification code


$rand = substr(str_shuffle($alphanum), 0, 5);



//assign session variable the verification code


$_SESSION[‘variable’]=$rand;



//create image from a given jpeg to write our code


$image = imagecreatefromjpeg("background.jpg");



//allocate the text color of the code


$textColor = imagecolorallocate($image, 0, 0, 0);



// write the code on the background image


imagestring ($image, 5, 5, 8, $rand, $textColor);



// send the content type header so the image is //displayed properly


header(‘Content-type: image/jpeg’);



//save the final image as image.jpg


imagejpeg($image,”image.jpg”);



echo “<img src=\”image.jpg\”>”;;



echo "Type the code you see in the image in the box below. (case sensitive)";



?>




<form action=check.php method=post>





<input name=code type=text > //place to type the code



<input type=submit> //submit button




</form>







With that done, we must now create a new file and name it check.php and see if it works.



//check.php



Session_start();



//retrieve the posted variable via forn


$typed=$_POST[‘code’];



//retrieve the session variable


$var=$_SESSION[‘variable’];



echo “The verification code was $var and you typed $typed”;



if($var==$typed)


{


echo “you are verified”;


}



?>




There you are, with your own image verification tool.



6 comments:

Unknown said...

Very useful! Thank you!

Anonymous said...

Good day !.
You may , probably very interested to know how one can collect a huge starting capital .
There is no initial capital needed You may start to receive yields with as small sum of money as 20-100 dollars.

AimTrust is what you haven`t ever dreamt of such a chance to become rich
The firm incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

Its head office is in Panama with structures around the world.
Do you want to become an affluent person?
That`s your choice That`s what you wish in the long run!

I`m happy and lucky, I began to get income with the help of this company,
and I invite you to do the same. It`s all about how to select a correct companion utilizes your funds in a right way - that`s it!.
I take now up to 2G every day, and my first deposit was 1 grand only!
It`s easy to get involved , just click this link http://ozazovojaj.mindnmagick.com/ylelylox.html
and go! Let`s take our chance together to become rich

Anonymous said...

Good day !.
You re, I guess , perhaps very interested to know how one can make real money .
There is no need to invest much at first. You may commense to receive yields with as small sum of money as 20-100 dollars.

AimTrust is what you need
The firm incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with structures around the world.
Do you want to become an affluent person?
That`s your chance That`s what you desire!

I`m happy and lucky, I started to take up income with the help of this company,
and I invite you to do the same. It`s all about how to choose a proper companion who uses your savings in a right way - that`s it!.
I earn US$2,000 per day, and what I started with was a funny sum of 500 bucks!
It`s easy to get involved , just click this link http://ynebibyduk.the-best-free-web-hosting.com/qupemuq.html
and lucky you`re! Let`s take our chance together to get rid of nastiness of the life

Anonymous said...

Good day, sun shines!
There have been times of troubles when I didn't know about opportunities of getting high yields on investments. I was a dump and downright pessimistic person.
I have never imagined that there weren't any need in large starting capital.
Nowadays, I'm happy and lucky , I begin take up real money.
It gets down to select a proper partner who utilizes your money in a right way - that is incorporate it in real business, and shares the profit with me.

You may get interested, if there are such firms? I have to tell the truth, YES, there are. Please get to know about one of them:
http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Anonymous said...

Genial brief and this fill someone in on helped me alot in my college assignement. Gratefulness you seeking your information.

Anonymous said...

How do you preserve out the burgeoning viruses and Trojan horses, prowling in the cyberspace? There is any quantity of programs like spam, adware, adware, malware and hackers all set to assault at the earliest chance. With out the understanding of web consumers, credit score card figures or passwords can move onto the wrong hands. These safety issues require to be dealt with urgently and listed here are two crucial suggestions for you.

1. The first action to shield your pc is to set up a good virus scanner and firewall. Let us confront the details, Microsoft's firewall is just not adequate, and so are from your ISP and modems. No modem arrives with developed in antivirus computer software. Thus you have to acquire and set up a single. You can choose from a quantity of virus scanners and firewalls, but dependable between them are: Norton, AVG, McAfee, and ZoneAlarm. AVG and ZoneAlarm are free software.

two. A [url=http://scrapeboxfaq.com/where-can-i-get-private-proxies ]http://www.scrapebox.com [/url] will help online protection. This server hides your IP, which is special and unchangeable even if you want to. This IP identity ought to be held magic formula. If a cybercriminal accesses it, he can get your facts, and use it to your detriment.

In this whole method a proxy works discreetly. When you try to access a backlink, the browser informs the server in regular situations, whilst a proxy server guarantees that it will get this info 1st, and filters the info. Even if the world wide web server attempts to know your IP, it will only be accessing the IP of the proxy server. Nevertheless, the proxy server can entry all your info, and therefore you must go for a proxy server which you can rely on.

Given that Proxy Servers support Online Protection., you must know how to established up one particular. Faulty set up can direct to an unsafe proxy - consequently very carefully comply with the different methods.

1. A excellent and anonymous proxy is a need to. The Web has a total great deal of paid and free of charge proxy servers. Whilst paid proxies offer far better anonymity, free proxies can also be opted. You should zero in on a great Proxy Server which will help your On the internet Safety.

2. Following you have decided on your proxy, you need to have to configure your Web browser to use it. This process is diverse from one to another browser. If you are employing Web Explorer, here is how to set up the proxy. Open up World wide web Explorer, go to Internet link, click on link, pick the proper button for LAN or dial-up connection, and click on the configurations. Empower use a proxy server, and sort IP of proxy - which you should have mentioned on a paper, just before beginning to install, followed by port in the respective fields. When you simply click on "Okay" two times, you are now obtaining a Proxy Server for On-line Stability.

three. It is not ample even immediately after you have completed configuring your browser. You just are unable to forget it. If you have opted for a cost-free Proxy, it could not be operating after some time. As a result, you really should preserve an eye on the proxy, and remain secure. Use Proxy Servers for On-line Security and it will reduce you of great deal of anxieties.