Learn Hosting,WebDev,Scripting Make Money Hosting, WebDev,Scripting
Home :: HostScreamer.com
 
   

How to implement CAPTCHA with PHP

October 11th, 2007

by Encaps

How to implement CAPTCHA with PHP and GD

So, you have a public submission form on your website (contact page, forum submission,blogs comment form) and need to prevent spam by auto-submitters. Common way to do this is to implement CAPTCHA – an image with randomly generated string (quote from Wikipedia, free online enciclopedia: “A CAPTCHA is a type of challenge-response test used in computing to determine whether the user is human. “CAPTCHA” is an acronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”, trademarked by Carnegie Mellon University.”)

Simple and quick PHP solution for implemet CAPTCHA:

1) Make a PHP script which will generate a CAPTCHA image:
[?php
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
function _generateRandom($length=6){
$_rand_src = array(
array(48,57) //digits
, array(97,122) //lowercase chars
// , array(65,90) //uppercase chars
);
srand ((double) microtime() * 1000000);
$random_string = "";
for($i=0;$i<$length;$i++){
$i1=rand(0,sizeof($_rand_src)-1);
$random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
}
return $random_string;
}
$im = @imagecreatefromjpeg(“captcha.jpg”);
$rand = _generateRandom(3);
$_SESSION['captcha'] = $rand;
ImageString($im, 5, 2, 2, $rand[0].” “.$rand[1].” “.$rand[2].” “, ImageColorAllocate ($im, 0, 0, 0));
$rand = _generateRandom(3);
ImageString($im, 5, 2, 2, ” “.$rand[0].” “.$rand[1].” “.$rand[2], ImageColorAllocate ($im, 255, 0, 0));
Header (‘Content-type: image/jpeg’);
imagejpeg($im,NULL,100);
ImageDestroy($im);
?]

2) Add the following line at the top of the page where you need to implement the CAPTCHA (there should not be any output before this line, except you use php functions ob_start() and ob_end_flush() to turn on output buffering):
3) Add the following line for check is CAPTCHA entered by visitor valid, before the line where you will proceed submitted message:
[?php if($_SESSION["captcha"]==$_POST["captcha"])
{
//CAPTHCA is valid; proceed the message: save to database, send by e-mail …
echo ‘CAPTHCA is valid; proceed the message’;
}
else
{
echo ‘CAPTHCA is not valid; ignore submission’;
}?]

4) Finaly add the CAPTCHA to the form:
[img xsrc="captcha.php" mce_src="captcha.php" alt="captcha image"][input type="text" name="captcha" size="3" maxlength="3"]

P.S.
notice:

- the tags “[?" and "?]” in code samples above should be replaced to the “”, you may download original article and code sample here: free download php captcha demo

- requirements: a webserver (windows or linux no matter) with PHP engine, with GD (graphic library) support, you may check your php settings with phpinfo() function

- you need some blank jpg image for use it as background for CAPTCHA string
About the Author 4-year PHP developer

[tags]captcha, php, programming, captcha with php, php solution, contact page, forum submission,blogs comment form[/tags]




Bravenet hosting is horrible

October 10th, 2007

by Jayden Smith

Bravenet Hosting Review

During my travels across the magical realm that we call the internet, I have come seen so many website hosting companies. From some of the biggest and well-known companies, to some of the smallest on the net. One day I was reading through a forum, where I was recommended to try out this company; ‘Bravenet’. With all the positive reviews that this company was getting, I thought that I might give it a try. Their site looked somewhat professional and their prices were reasonable.

So, I investigated further; I decided to buy a domain name through this company and have my new website hosted via their free hosting service. Big mistake. After purchasing the domain name, for a reasonable price, I then uploaded my website. Everything was running fine for a day or two, then out of nowhere, my website was full of ads. It seems that Bravenet like to destroy sites by spamming them with ads. Though, this is ok, because they are sponsoring my website hosting with ads, fair enough I thought.

Then, without warning there was a pop-up ad, followed by another and another. These pop-up ads were selling pharmacy pills and anything else you could image. I was horrified; my little site has turned in to an abomination. These ads also retarded the coding of my site, so it never functioned correctly after that.

Now, you’re probably saying that ads on a free website are ok. Well, they most certainly are, however pop-up ads are going a bit too far. Considering that other free website hosting companies such as 50webs (highly recommended) do know use forced ads on their websites. Meaning your precious little site will remain unviolated.

That being said, if you’re looking for free website hosting I highly recommend that you avoid Bravenet and use another company. 50webs and similar companies are the perfect substitute. As for my website, I’ve since taken it down and changed the hosting over to 50webs. Unfortunately I had to register another domain name, since Bravenet would not let me transfer my original one. This experience has taught me never to use Bravenet again.
About the Author Be Successful Online has all the resources to help you with anything from Website Hosting Companies to Tools for success

Web Hosting

[tags]bravenet, bravenet hosting, bravenet hosting review, web hosting, hosting review[/tags]