比较常用的PHP四位数字验证码,用session实现

来源:互联网 发布:大数据整体解决方案 编辑:程序博客网 时间:2024/05/16 05:59

参考的网上的,列出一些说明

<?php

  session_start();
  //生成验证码图片
  Header("Content-type: image/PNG");
  $im = imagecreate(44,18);
  $back = ImageColorAllocate($im, 245,245,245);
  imagefill($im,0,0,$back); //背景

  srand((double)microtime()*1000000);
  //生成4位数字
  for($i=0;$i<4;$i++){
    $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
    $authnum=rand(1,9);
    $vcodes.=$authnum;
    imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
  }

  for($i=0;$i<100;$i++) //加入干扰象素
  {
    $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
  }
  ImagePNG($im);
  ImageDestroy($im);

  $_SESSION['VCODE'] = $vcodes;

?>

Content-type的说明:http://download.csdn.net/source/2024307

Header:

送出 HTTP 协议的标头到浏览器

语法: int header(string string);

返回值: 整数

函数种类: 网络系统

标头 (header) 是服务器以 HTTP 协议传 HTML 资料到浏览器前所送出的字符串,在标头与 HTML 文件之间尚需空一行分隔。在PHP 中送回 HTML 资料前,需先传完所有的标头

注意传统的标头一定包含下面三种标头之一,并只能出现一次。

· Content-Type: xxxx/yyyy

· Location: xxxx:yyyy/zzzz

· Status: nnn xxxxxx

在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。

范例一本例用来重导用户到 PHP 的官方网站。
<?php
Header("Location: http://www.php.net");  
exit;
?>
范例二欲让用户每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
范例三让用户的浏览器出现找不到文件的信息。
<?php
header("Status: 404 Not Found");
?>
范例四: bill@evil.inetarena.com (28-Apr-1999) 提供让用户下载文件的范例。
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=some-file.tar.gz");
header("Content-Description: PHP3 Generated Data");