CodeIgniter-----验证码AJAX实现

CI框架用controller自己写验证码     首先嫌麻烦的话这个框架有一个自带的库 那个很很简单 但是很难看   大概是这样的

   

   $this->load->helper('captcha');
        $vals = array(
    'img_path'  => './captcha/',
    'img_url'   => base_url().'/captcha/',
    'img_width' => '150',
    'img_height'    => 30,
    'expiration'    => 7200,
    'word_length'   => 8,
    'font_size' => 16,
    'img_id'    => 'Imageid',
    'pool'      => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
    'colors'    => array(
        'background' => array(255, 255, 255),
        'border' => array(255, 255, 255),
        'text' => array(0, 0, 0),
        'grid' => array(255, 40, 40)
    )
);
$cap = create_captcha($vals);
echo $cap['image'];

自己定义的验证码 然后CI框架貌似必须生成本地的引用文件名不能像引用php一样引用方法,即不能<img src=....... axxxx();这样  

controller部分

public function c_test()
    {
        $image=imagecreatetruecolor(100, 30);                                   //定义画板  左上角的坐标
        $bgcolor=imagecolorallocate($image,255,255,255);            //三原色;255*3是白板
        imagefill($image,0,0,$bgcolor);                                                //填充颜色


       $captch_code="";                                                                           //保存验证码的变量
for($i=0;$i<4;$i++){                                                                                     //随机4个字符 从数组里面找
        $fontsize=6;                                                                                     //字符大小
        $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120)); //字符的颜色 三个随机  0-120是深色画板 为了在白板上显示清晰
        $data='abcdefghijklmnpqrstuvwxy13456789';                                               //数组 去掉了2z 和o0
        $fontcontent=substr($data,rand(0,strlen($data)),1);                                          //开始存
        $captch_code.=$fontcontent;                                                                     //将获取的放到$captch_code变量里
      $x=($i*100/4)+rand(5,10);                                                             //x y 的坐标  x是每次像右移动25
      $y=rand(5,10);
      imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);              //给图片填充颜色
}
for($i=0;$i<200;$i++)
{
    $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));             //这里是生成随机的点 200个
    imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}


for($i=0;$i<3;$i++){
    $linecolor=imagecolorallocate($image, rand(8,220), rand(80,220), rand(80,220));                    //这里是生成随机的线200个
    imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);                               
}
$_SESSION['captch_code']=$captch_code;                                                                                      //$_SESSION存的验证码信息
$time = date("Ymdhisa");                                                                                                               //用即时的时间当作文件名
imagepng($image,"image/".$time.".png");                                                                     //生成图片到路径下
echo  $time;                                                                                                //返回文件名字
}


View部分js

<script src="http://localhost:/secondproject/js/jquery.js"></script>      //导入jquery文件 路径
<script type="text/javascript">
 function cs()                                                                                  //ajax函数(js标签内)                 加载页面的时候      然后onclick  ="cs()
   { 
  $.ajax({
    url:"c_cap",                                                      //没有需要传的值 所以直接写方法就行  c_cap是上面ctroller的方法
        success:function(result)
        {  
          var kkk=result;                                                                        //ajax传过来的 前面有个 空格/回车  这里给他消掉
            kkk=kkk.substr(4);
             document.getElementById("div1").innerHTML="<img src='http://localhost/secondproject/image/"+kkk+".png'>";        //div1是放验证码的div标签i的d
       },
        error:function()
        {alert("error");}
    })
   }
</script>
<button οnclick="cs()">看不清</button>
<div id="div1">
</div>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值