Forum Moderators: coopster
Then in the destination script you check that the security code entered matches the corresponding filename.
Something like that ;) It could be done quite a few ways, I imagine.
So my main page where the image display has the following:
...
<? $num_code = rand(1000,9999);?>
<img src="image.php?num_code=<?=$num_code?>" />
...
<form action="validate.php" ...>
<input type="hidden" name="num_code" value="<?=$num_code?>" />
...
image.php goes like this:
$code = $_GET['num_code'];
// just so I don't use num_code straigth in there
$code = substr(md5($code),16,4);
$png = ImageCreate(100,50);
$bgColor = ImageColorAllocate($png,0,0,0);
$textColor = ImageColorAllocate($png,255,128,128);
ImageFilledRectangle($png,0,0,100,50,$bgColor);
ImageString($png,9,20,20,$code,$textColor);
// check your GD library version (I use png here because GIF is supported in read only in mine
header("content-type: image/png");
ImagePng($png);
and then process.php:
if (substr(md5($_POST['num_code']),16,4) == $_POST['user_input'])
{
// valid
} else {
// invalid
}
again this is quite basic.
hope that helps
mavherick
<?php
$code = $_GET['num_code'];
// just so I don't use num_code straigth in there
$code1 = substrmd5($code,16,1);
$code2 = substrmd5($code,17,1);
$code3 = substrmd5($code,18,1);
$code4 = substrmd5($code,19,1);
$png = (ImageCreate(100,50);
$bgColor = ImageColorAllocate($png,0,0,0);
$textColor = ImageColorAllocate($png,255,128,128);
ImageFilledRectangle($png,0,0,100,50,$bgColor);
ImageString($png,9,20,15,$code1,$textColor);
ImageString($png,9,40,25,$code2,$textColor);
ImageString($png,9,60,15,$code3,$textColor);
ImageString($png,9,80,25,$code4,$textColor);
header("content-type: image/png");
ImagePng($png);
?>
I've updated my own with Birdman's improvement but had trouble with it at first, then realized it's just a few typos, but by that time rewrote it with arrays like he suggested so here it goes (image.php):
$code = substr(md5($_GET['num_code']),16,4);
for($i=0; $i<=3; $i++) { $code_letters[$i] = substr($code,$i,1); }
$png = ImageCreate(100,50);
$bgColor = ImageColorAllocate($png,0,0,0);
$textColor = ImageColorAllocate($png,255,128,128);
ImageFilledRectangle($png,0,0,100,50,$bgColor);
for($i=0; $i<=3; $i++) {
($i % 2)? $yCoor = 15 : $yCoor = 25;
$xCoor = ($i + 1) * 20;
ImageString($png,9,$xCoor,$yCoor,$code_letters[$i],$textColor);
}
header("content-type: image/png");
ImagePng($png);
mavherick