以下:
index.php
<?php
<html>
<head>
</head>
<body>
<FORM name="form1" action="save.php" method="post">
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width="100%" align=center borderColorLight=#cccccc border=1>
<TR vAlign=center align=middle>
<TD width="70%" height=20><input type="text" name="testyan" value=""></TD>
<TD width="30%" ><img src="testyan.php?" align=absmiddle></TD></TR>
</TABLE>
<center><input type="submit" name="s_snatch" value="提交"></center>
</body>
</html>
?>
注意以上代码!
testyan.php:
<?
function creatvalidateimg ($width, $height, $space, $size, $disturb_num, $sname = '')
{
$left = 1;
$top = 0;
$authstr = mt_rand (1000, 9999);
if (($sname != ''))
{
$_SESSION[$sname] = $authstr;
}
$image = imagecreate ($width, $height);
$colorList[] = imagecolorallocate ($image, 15, 73, 210);
$colorList[] = imagecolorallocate ($image, 46, 175, 7);
$colorList[] = imagecolorallocate ($image, 231, 185, 3);
$colorList[] = imagecolorallocate ($image, 230, 16, 4);
$colorList[] = imagecolorallocate ($image, 199, 88, 35);
$colorList[] = imagecolorallocate ($image, 173, 114, 61);
$colorList[] = imagecolorallocate ($image, 55, 179, 179);
$colorList[] = imagecolorallocate ($image, 171, 50, 153);
$colorList[] = imagecolorallocate ($image, 254, 52, 138);
$colorList[] = imagecolorallocate ($image, 0, 0, 145);
$colorList[] = imagecolorallocate ($image, 0, 0, 113);
$colorList[] = imagecolorallocate ($image, 228, 118, 237);
$colorList[] = imagecolorallocate ($image, 158, 180, 35);
$colorList[] = imagecolorallocate ($image, 255, 36, 36);
$colorList[] = imagecolorallocate ($image, 255, 72, 72);
$colorList[] = imagecolorallocate ($image, 247, 179, 51);
$gray = imagecolorallocate ($image, 230, 230, 230);
imagefill ($image, 0, 0, $gray);
for ($i = 0; ($i < strlen ($authstr)); ++$i)
{
$colorRandom = mt_rand (0, (sizeof ($colorList) - 1));
imagestring ($image, $size, (($space * $i) + $left), $top, substr ($authstr, $i, 1), $colorList[$colorRandom]);
}
for ($i = 0; ($i < $disturb_num); ++$i)
{
$colorRandom = mt_rand (0, (sizeof ($colorList) - 1));
imagesetpixel ($image, (rand () % 70), (rand () % 10), $colorList[$colorRandom]);
}
header ('Content-type: image/PNG');
imagepng ($image);
imagedestroy ($image);
}
session_start ();
//$_SESSION['sessionValid'] = mt_rand (1000, 9999);
creatvalidateimg (48, 18, 10, 6, 30, 'ValidateCode');
?>
声明以上引用了本村的一个朋友的函数!
以下是save.php
<?
session_start ();
echo $testyan;
$testyan=$_POST["testyan"];
$sessionValid=$_SESSION[ValidateCode];
if($testyan!=$sessionValid)
{
echo "post[testyan]=".$testyan."n";
echo "SESSION[$sname]=".$sessionValid."n";
echo "no good";
}
else
{
echo "post[testyan]=".$testyan."n";
echo "SESSION[$sname]=".$sessionValid."n";
echo "good!";
}
//$_SESSION = array();
// Finally, destroy the session.
//session_destroy();
echo "<meta http-equiv=\"Refresh\" content=\"5; url=index.php\">";
?>