<?php
session_start();
$table=array(
'pic0'=>'鸟',
'pic1'=>'猫',
'pic2'=>'狗',
'pic3'=>'鱼'
) ;
$index=mt_rand(0,3);
$value=$table['pic'.$index];
$_SESSION['authcode']=$value;
//$filename=dirname(__FILE__).'\\pic'.$index.'jpg';
$filename=dirname(__FILE__).'\\pic'.$index.'.jpg';//__FILE__这里两条下划线不要忘了,相邻的两个“.”差点搞死我
$contents=file_get_contents($filename);
//header('content-type:image/jpg');//输出内容前??
echo $contents;
//end
imagedestroy();
在form_xxx.php 中 或者将 php代码 包含到 <body> 中,或者在 php代码最前面加上 header('content-type:text/html;charset=utf-8'); 这样浏览器才能正确处理 $_SESSION['authcode'] 和 $_REQUEST['authcode']中可能出现的汉字 captch_img.php 中的代码: session_start(); //开启 session 功能 $table = array( //制作 验证图 验证码 对照表 'pic0'=>'猫', 'pic1'=>'狗', 'pic2'=>'蛇', 'pic3'=>'马' ); $index = rand(0,3); //生成随机数 $value = $table['pic'.$index]; //获取对应验证码 $_SESSION['authcode'] = $value; //保存验证码 $filename = dirname(__FILE__).'\\pic'.$index.'.jpg'; //获取验证图文件路径 $contents = file_get_contents($filename); //获取验证图内容 header('content-type:image/jpg'); //填写报头 echo $contents; //输出验证图