您的位置:首页 > 编程语言 > PHP开发

php代码实现随机动态验证码

2020-07-18 04:44 573 查看

设置字母混合效果和干扰元素
通过SESSION存储验证信息

session_start();
$width = 150;
$height = 40;
$image = imagecreatetruecolor($width, $height);$bgcolor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgcolor);
$cap_code = "";
for ($i = 0; $i < 4; $i++) {
$fontsize = 30;
$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
$data = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789';    $fontcontent = substr($data, rand(0, strlen($data)), 1);
$cap_code .= $fontcontent;
$x = ($i * 150 / 4) + rand(5, 10);
$y = rand(5, 10);
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);}
$_SESSION['code'] = $cap_code;
for ($i = 0; $i < 300; $i++) {
$inputcolor = imagecolorallocate($image, rand(50, 200), rand(20, 200), rand(50, 200));
imagesetpixel($image, rand(1, 149), rand(1, 39), $inputcolor);
}
for ($i = 0; $i < 4; $i++) {
$linecolor = imagecolorallocate($image, rand(20, 220), rand(20, 220), rand(20, 220));
imageline($image, rand(1, 149), rand(1, 39), rand(1, 299), rand(1, 149), $linecolor);
}
header('Content-Type:image/png');
imagepng($image);

使用验证码, 验证用户的提交操作
还有一个css小特效哦

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>展现你实力的时候到啦</title>
<style>
#myDIV{
width:200px;
height:65px;
background:red;
animation:mymove 5s infinite;
-webkit-animation:mymove 5s infinite;
}
@keyframes mymove
{
from {background-color:green;}
to {background-color:yellow;}}
@-webkit-keyframes mymove
{    from {background-color:yellow;}
to {background-color:green;}
}
</style>
</head>
<body>
<div id="myDIV">
<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_REQUEST['code'])){    session_start();
if ($_REQUEST['code'] == $_SESSION['code']){
echo "对于如此优秀的你来说考试不就像吹牛皮一样说破就破吗?";
}else{        echo "你是猪吗?";    }
exit();}
?>
</div>
<form>
<p>验证码:<img src="sxj.php" onClick="this.src='sxj.php?nocache='+Math.random()" style="cursor:hand" alt="点击换一张"/>点击图片可更换验证码</p>
<p>请输入图片中的内容:<input type="text" name="code" value=""/></p>
<p><input type="submit" width="20px" height=19px value="提交"></input></p>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: