shenyg 发表于 2017-3-20 13:11:32

php 验证码

  checkcode.php
  ====================
  <?php
  session_start();
  $funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imageline','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
  if(!function_exists('ob_gzhandler'))
ob_clean();
  //create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
}
$radomstring = $const . $vow .$const . $const . $vow . $const . $vow . $const;
   $_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
   //set up image, the first number is the width and the second is the height
$imageX = strlen($radomstring)*8;//the image width
$imageY = 20;//the image height
$im = imagecreatetruecolor($imageX,$imageY);
  //creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);
  //fill image with bgcolor
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//writes string
   imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr, 'fonts/ALGER.TTF ', $string);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr, 'fonts/ARIALNI.TTF', $string);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr, 'fonts/ALGER.TTF', $string);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr, 'fonts/arial.ttf', $string);

//strikethrough
  $border = imagecolorallocate($im, 133, 153, 193);
//imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
  $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//random shapes
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png/r/n");
  
imagepng($im);
imagedestroy($im);
?>
  ===========================================
  调用生成的 验证码
  <img src="checkcode.php" id="checkcode"
style="cursor:pointer;"
alt="验证码,看不清楚?请点击刷新验证码" align="absmiddle" class="color_1" />
  <input type="button"value="测试"/>
<script type="text/javascript">
function test(){
window.location.href = "valid.php";
}
</script>
  ==================
  valid.php
  <?php
session_start();
$code = $_SESSION['checkcode'];
echo $code."<br>";
?>
页: [1]
查看完整版本: php 验证码