PHP简单的图片验证码实现

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//第一段代码生成随机图片------code2.php

<?php
//开启session以用于保存生成的内容
session_start();
$table=array(
              "pic0"=>"dog",
              "pic1"=>"cat",
              "pic2"=>"fish",
              "pic3"=>"bird"
);
$index=rand(0,3);
$value=$table['pic'.$index];    
$_SESSION['authcode']=$value;
//得到随机生成的图片名
$filename=dirname(__FILE__).'/pic'.$index.'.jpg';
$content=file_get_contents($filename);
header('Content-type:image/jpg');
echo $content;
?>


//第二段代码实线表单的验证

<?php
if(isset($_REQUEST['authcode'])){
session_start();
if($_REQUEST['authcode']==$_SESSION['authcode']){
echo "right";
}else{
echo "sad story<br/>";
echo  $_REQUEST['authcode'];
}    
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="./code1.php" method="post">
    <p>picture:<img  id="cap_img"  border="1" src="./code2.php?r=<?php echo rand(); ?>" width="100" height="30" /> </p>
     <br/>
     //通过js切换另外一张图片
     <a href="javascript:void(0)" onclick="document.getElementById('cap_img').src='./code2.php?r='+Math.random()">change picture</a>
   <p>words:<input type="text"  name="authcode" value="" /></p>
   <p><input type="submit" value="sub"></p>
</form>
</body>
</html>
?>


/*代码的路径是linux的路径*/