php验证码实现代码:
php代码:
public function get_code(){
header("content-type:image/jpeg");
//session_start();
$img = imagecreatetruecolor(75,25);
$bg = imagecolorallocate($img,rand(100,255),rand(100,255),rand(100,255));
imagefilledrectangle($img,0,0,75,25,$bg);
$str = "";//存储验证码
$trueCode = "";//真实的验证码
for($i=0;$i<4;$i++)
{
$r = rand(0,9);
$str .= $r." ";
$trueCode .= $r;
}
//$_SESSION["myCode"] = $trueCode;
$this->session->set_userdata(array('myCode'=>$trueCode));
// /echo $trueCode;
$fg = imagecolorallocate($img,rand(0,50),rand(0,150),rand(0,150));
imagestring($img,6,5,5,$str,$fg);
for($i=0;$i<4;$i++)
{
$lineColor = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imageline($img,rand(0,75),rand(0,25),rand(0,75),rand(0,25),$lineColor);
}
for($i=0;$i<100;$i++)
{
$lineColor = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img,rand(0,75),rand(0,25),$lineColor);
}
imagejpeg($img);
//return imagejpeg($img);
}
HTML代码:
<p style="line-height: 20px;">Security Code:</p>
<input type="text" name="check_code" value="<?php echo $check_code?$check_code:'';?>"/>
<img src="<?php echo site_url('reg_login/get_code')?>" border="0" οnclick="this.src='<?php echo site_url('reg_login/get_code/')?>?random='+Math.random();" align="absmiddle" alt="Can't see?Change it!" title="Can't see?Change it!"/>
谢谢关注websites博客!