Security digits in php (captcha) for login pages

Simplest captcha generator:

Add this code to the login form:

<?

if (isset($_POST[“code”]) and  isset($_SESSION[“seccode”])  and $_POST[“code”]==$_SESSION[“seccode”])
{

echo ‘Correct security code’;’

}

else

{

echo ‘Incorrect security code’;

}

// 5 digit security number.

$t1=rand(1,9);$t2=rand(1,9);$t3=rand(1,9);$t4=rand(1,9);$t5=rand(1,9);
$code=$t1.”.$t2.”.$t3.”.$t4.”.$t5;
$_SESSION[“seccode”]=$code;

?>

<input name=”code” type=”text” id=”code” maxlength=”5″> <img src=”cap.php” />

Create cap.php file in root folder and add this code there:

<?php
session_start();

$image = writeToImage(‘seccap.jpg’, urldecode($_SESSION[“seccode”]));

imagejpeg($image);

function writeToImage($imagefile, $text){
if(file_exists($imagefile))
{
$image = @imagecreatefromjpeg($imagefile);

$text_color = imagecolorallocate($image, 13, 14, 191);

imagestring($image, 6, 15, 3,  “$text”, $text_color);
}
else
{
$image  = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($image, 255, 255, 255);

$tc  = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 150, 30, $bgc);
imagestring($image, 1, 5, 5, “Error loading $imagefile”, $tc);
}
return $image;

}

?>

Create small jpg file named ‘seccap.jpg’ with wished background, and upload it to a root folder of your website.

CAPTCHA SECURITY FOR YOUR WEBSITE IS READY!!! 🙂

Yeni yazı olanda xəbər tutmaq üçün

* indicates required

Bir cavab yazın

Sizin e-poçt ünvanınız dərc edilməyəcəkdir. Gərəkli sahələr * ilə işarələnmişdir