Jump to content
alexarpad2003

script captcha

Recommended Posts

Posted (edited)

Creati fisierul captcha.php in care o sa avem codul care genereaza o imagine captcha :


<?php
session_start();// graphic
header("content-type: image/png"); // type
$image = imagecreate(100, 46); // size
// BACKGROUND
$achtergrond = imagecolorallocate($image, 234, 234, 234);
// BLACK
$zwart = imagecolorallocate($image, 0, 0, 0);
// GRAY
$grijs = imagecolorallocate($image, 200, 200, 200);
// GREEN
$groenb = imagecolorallocate($image, 133, 163, 174);
// BORDER
$bordercolor = imagecolorallocate($image,0,0,0);
// draw some lines
for($i=0;$i<12;$i++)
{
$x1 = rand(0,100); // random x- en y coordinaten voor de lijnen
$y1 = rand(0,100);
$x2 = rand(0,100);
$y2 = rand(0,100);
imageline($image,$x1,$y1,$x2,$y2,$grijs);

}
// random text
$input = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0');

$rand_keys = array_rand($input, 5); // 5 random tekens selecteren uit de array
$int_x = 15; // startp x
$int_y = 25; // startp y
$tekens = '';
for($i=0; $i<5; $i++)
{//place signs
$font_kleur1 = rand(50,200); // variate colors
$font_kleur2 = rand(50,200);
$font_kleur3 = rand(50,200);
$random_kleur = imagecolorallocate($image, $font_kleur1, $font_kleur2, $font_kleur3);
// place some extra pixels
for($y=0;$y<50;$y++)
{
$pixel_x_rand = rand(0,100); // random coordinaten
$pixel_y_rand = rand(0,50);
imagesetpixel($image, $pixel_x_rand, $pixel_y_rand, $random_kleur);
imagesetpixel($image, $pixel_x_rand+1, $pixel_y_rand+1, $random_kleur);
$y++;
}
$grootte_tekst = rand(13,15); // text size
$font = 'verdanab.ttf'; // TrueType Font loaded from file
$angle_rand = rand(-20,20);
imagettftext($image, $grootte_tekst, $angle_rand, $int_x, $int_y, $random_kleur, $font, $input[$rand_keys[$i]]);
$regels = '';
$tekens .= $input[$rand_keys[$i]];
$int_x = $int_x + 15;
$random_y = rand(5,15);
$int_y = ($int_y >= 26 ? $int_y - $random_y : $int_y + $random_y);

}
imagerectangle($image,0,0,99,45,$bordercolor);
$_SESSION['captcha'] = $tekens;
imagepng($image);
imagedestroy($image);
?>

Aveti nevoie de fontul verdanab.ttf pe care il puteti downloada de aici si care trebuie sa fie in acelasi folder cu captcha.php .

Urmatorul cod poate fi pus intr-o pagina de register, upload, login, unde vreti voi sa se afle imaginea captcha si formul cu inputul ce face validarea captcha-ului :


<?php
session_start();
if ($_POST['submit'])
{
if ($_POST['captcha']==$_SESSION['captcha']) header('Location:http://www.google.ro');
else $eroare='codul nu este bun';
}

?>

<img src="captcha.php" alt="CAPTCHA" /><br/>
<form name="formular" method="post" action="">
<input type="text" name="captcha" />
<input type="submit" value="submit" name="submit"><br/>
<?php echo $eroare;?>

Sursa : www.opensc.ws

Edited by alexarpad2003

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...