GD Img Validatie upate 1
Een GD Img Validatie Systeempje tegen spam bots waar mensen de code die op de img verschijnt in moeten voeren om verder te kunnen gaan (met registreren oid) Het schript maakt gebruik van .htaccess om de .png als .php te laten parsen. Zet de .htaccess in dezelfde map als de image.png staat. Het is ook nodig om het font (.ttf) in dezelfde map als de image.png te zetten anders werkt je image script niet. Ik ben nog niet zo heel erg bedreven met GD dus alle op en aanmerkingen zijn van harte welkom. Dit is v1 van het script, het kan zijn dat het nog niet helemaal waterdicht is. Update: De security code in de image.png staat nou per karacter iets gedraait en hebben per char een random kleur. Er worden ook wat eclypsen op de achtergrond getekend die ook allemaal een eigen random kleur krijgen toegewezen. Enjoy, Grtz Nano
======================
#--> de .htaccess (zet deze in dezelfde map als de image.png staat)
======================
<FilesMatch "\.(png)$" >
ForceType application/x-httpd-php
</FilesMatch>
======================
#--> index.php (just a test file :))
======================
[code]
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>GD Img Validatie Test</title>
<script type="text/javascript" language="javascript1.5">
<!--
function change_img()
{
num = Math.random()
num = num.toString();
num = num.substr(2, num.length);
document.getElementById('img').src = 'image.png?hash='+num;
return false;
}
//-->
</script>
<style type="text/css">
body, input {
font-family:tahoma;
font-size:12px;
}
form {
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<?php
if(!isset($_POST['submit']))
{
?>
<form action="index.php" method="post">
Neem de text over om verder te gaan<br /><br />
<img id="img" style="border:1px solid #BEDFFB;" src="image.png" /> <a href="index.php" onclick="return change_img();">Change</a><br />
<input type="text" name="num" size="8" maxlength="5" onkeyup="this.value=this.value.toUpperCase();" style="margin-top:4px;" /><br /><br />
<input id="submit" type="submit" name="submit" value="Register" />
</form>
<?php
}
else
{
if(isset($_SESSION['num']) && strtoupper($_POST['num']) == $_SESSION['num'])
echo '<img src="goed.gif" alt="" /> Access Granted<br /><br />';
else
echo '<img src="fout.gif" alt="" /> Access Denied<br /><br />';
echo '<a href="index.php">back</a>';
}
?>
</body>
</html>
[/code]
======================
#--> image.png (een gewone php file ge-renamed naar .png)
======================
<?php
/********
* Starting Session
********/
session_start();
/********
* Function for creating random colors
********/
function random_color($im, $min, $max)
{
return imagecolorallocate($im, mt_rand($min, $max), mt_rand($min, $max), mt_rand($min, $max));
}
/********
* Some vars
********/
$im_width = 160;
$im_height = 40;
$font = 'font.ttf';
$font_size = 12;
/********
* Create img and draw background color
********/
$im = imagecreate($im_width, $im_height);
imagecolorallocate($im, 252, 252, 252);
/********
* Draw eclypses - (mischien nog effe kijken of ik ze variable kan maken, dat ze niet altijd op dezefde plaat komen)
********/
$color = random_color($im, 220, 250);
imageellipse($im, 10, 10, 110, 30, $color);
imageellipse($im, 11, 11, 111, 31, $color);
$color = random_color($im, 220, 250);
imageellipse($im, 110, 10, 110, 40, $color);
imageellipse($im, 111, 11, 111, 41, $color);
$color = random_color($im, 220, 250);
imageellipse($im, 90, 45, 140, 50, $color);
imageellipse($im, 90, 46, 141, 51, $color);
/********
* Make security code and draw to img (random colors per char)
********/
$chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$max = strlen($chars) - 1;
$num = null;
for($i=0; $i<5; $i++)
{
$char = $chars{mt_rand(0, $max)};
$rotation = ($i % 2) ? -6 : 6;
imagettftext($im, $font_size, $rotation, $i * 26 + 20, 26, random_color($im, 50, 240), $font, $char);
$num .= $char;
}
/********
* Store Security code in Session
********/
$_SESSION['num'] = $num;
/********
* Output Image
********/
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Reacties
0