
When running a website, it is very important to protect yourself from spammers. If you're working on a PHP website, there are many ways to do this. Here are the top seven methods to stop spamming on your website. |
1. CAPTCHA: You probably already know what CAPTCHA is. Websites often ask you to enter text in order to identify yourself, this is done through CAPTCHA. You need to add the CAPTCHA image and a text input to whatever you’re trying to protect on your website.
In order to implement CAPTCHA, you need to first install the Text_CAPTCHA class. In addition, you need the Text_Password and Image_Text classes to be installed too.
Use the following commands in order to install Pear’s Text_CAPTCHA class.
# pear install -f Text_CAPTCHA
# pear install -f Image_Text
Then you need the captcha.php file, which is the script that will generate the CAPTCHA image. Further, you need the TrueType font to be kept in the same directory as captcha.php. Why? Because this is the fond that is used in order to write a secret phrase.
On Windows these fonts are usually present in: C:\Windows\Fonts directory.
This is how the captcha class looks,
require_once('Text/CAPTCHA.php');
session_start();
$phrase = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : 'Error';
$options = array('font_size' => 24, 'font_file' => 'georgia.ttf');
$cap = Text_CAPTCHA::factory('Image');
$cap->init(120, 60, $phrase, $options);
header('Content-type: image/png');
echo $cap->getCAPTCHAAsPNG();
?>
The code given above doesn’t perform any error checks. In case you want that, you will have to add it yourself. In addition, it will use the text “Error” if no phrase is found. In such a case, the user will never be able to cross the CAPTCHA message.
The PHP code for the form processor and the form are as follows. This is a very simple version and real form processing will have much more to it.
//
//session_start();
//
//if(isset($_POST['process'])){
// if(!isset($_SESSION['captcha']))
// die('Form accessed incorrectly');
//
// if(isset($_POST['captcha']) && $_POST['captcha'] == $_SESSION['captcha']){
// die('CAPTCHA text matched! Phrase was '.$_SESSION['captcha']);
//}else{
// die('CAPTCHA text did not match. Phrase was '.$_SESSION['captcha'].
// ', you entered '.$_POST['captcha']);
//}else{
// generate a new CAPTCHA phrase
// $_SESSION['captcha'] = substr(md5(uniqid(null)), 0, 4);
//}
//?>
//
//
//
//
//
//
No comments:
Post a Comment