php登录加验证码,php如何写注册登录验证码

6503ca649b5f81ce75b91175cdec6e31.png

authimg.php生成验证码的文件:

$IsLetter = false; //是否出现字母

session_start();

$Num = $_GET["n"];

//生成验证码图片

header("Content-type: image/PNG");

$im = imagecreate(46,20);

srand((double)microtime()*1000000);

$Red = rand(0,200);

$Green = rand(0,200);

$Blue = rand(0,200);

$Color = imagecolorallocate($im, $Red, $Green, $Blue);

$BackGround = imagecolorallocate($im, 255,255,255);

imagefill($im,0,0,$BackGround);

if($IsLetter)

{

$a = substr(md5($Num*10000000000000000),0,1);

$b = substr(md5($Num*10000000000000000),4,1);

$c = substr(md5($Num*10000000000000000),8,1);

$d = substr(md5($Num*10000000000000000),12,1);

}

else

{

$a = substr(hexdec(md5($Num*10000000000000000)),2,1);

$b = substr(hexdec(md5($Num*10000000000000000)),3,1);

$c = substr(hexdec(md5($Num*10000000000000000)),4,1);

$d = substr(hexdec(md5($Num*10000000000000000)),5,1);

}

$Authnum = strtoupper($a.$b.$c.$d);

$_SESSION["Authnum"] = $Authnum;

imagestring($im, 5, 5, 2, $Authnum, $Color);

for($i=0;$i<200;$i++) //加入干扰象素

{

$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

imagepng($im);

imagedestroy($im);

?>

需要显示验证码的文件:

session_start();

$AuthInput = $_POST["AuthInput"];

$action = $_GET["action"];

?>

function lyalert(){

if( addform.AuthInput.value=="") {

alert("请填写验证码");

document.addform.AuthInput.focus();

return false;

}

}

验证码:

if($action=='save')

{

if ($_SESSION["Authnum"] != $AuthInput)

{

?>

alert("验证码输入不正确");

}

else

{

?>

alert("提交成功!");

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成验证码的步骤如下: 1. 生成随机字符串:使用 `rand()` 函数生成一串随机数字或字母,长度为4-6位。为了避免出现易混淆的字符,可以使用字符集 `23456789abcdefghjkmnpqrstuvwxyz`。 2. 将随机字符串入到图片中:使用 `imagecreate()` 函数创建一张空白图片,再使用 `imagestring()` 函数将字符串入到图片中。 3. 添干扰线和噪点:使用 `imageline()` 函数添干扰线,使用 `imagesetpixel()` 函数添噪点。 4. 输出验证码图片:使用 `header()` 函数设置图片类型,然后使用 `imagepng()` 函数输出图片。 下面是一个简单的 PHP 验证码生成函数的示例: ``` <?php session_start(); function generateCaptcha() { $captcha = ""; $characters = "23456789abcdefghjkmnpqrstuvwxyz"; for ($i = 0; $i < 6; $i++) { $captcha .= $characters[rand(0, strlen($characters) - 1)]; } $_SESSION['captcha'] = $captcha; $image = imagecreate(100, 30); $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 0, 0, 0); for ($i = 0; $i < 5; $i++) { imageline($image, 0, rand(0, 30), 100, rand(0, 30), $text_color); } for ($i = 0; $i < 50; $i++) { imagesetpixel($image, rand(0, 100), rand(0, 30), $text_color); } imagestring($image, 5, 20, 8, $captcha, $text_color); header('Content-type: image/png'); imagepng($image); imagedestroy($image); } generateCaptcha(); ?> ``` 在页面中调用该函数即可生成验证码图片: ``` <img src="captcha.php" alt="Captcha"> ``` 在验证用户输入时,将用户输入的验证码与 `$_SESSION['captcha']` 中存储的验证码进行比较即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值