验证码文件GetVerifyImg.php

<?php
session_start();
function GetVerify($length)
{
$strings = Array('3','4','5','6','7','a','b','c','d','e','f','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y');
$chrNum = "";
$count = count($strings);
for ($i = 1; $i <= $length; $i++) { //循环随机取字符生成字符串
$chrNum .= $strings[rand(0,$count-1)];
}
return $chrNum;
}
$fontSize = 15; //定义字体大小
$length = 4; //定义字符串长度
$strNum = GetVerify($length); //获取一个随机字符串
$_SESSION['verify'] = $strNum; //付值给session
$width = 70; //定义图片宽度
$height = 24; //定义图片高度
$im = imagecreate($width,$height); //生成一张指定宽高的图片
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255); //生成背景色
$frameColor = imageColorAllocate($im, 150, 150, 150); //生成边框色
$font = realpath("arial.ttf"); //提取字体文件,开始写字
for($i = 0; $i < $length; $i++) {
$charY = ($height+9)/2 + rand(-1,1); //定义字符Y坐标
$charX = $i*15+8; //定义字符X坐标
//生成字符颜色
$text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200));
$angle = rand(-20,20); //生成字符角度
//写入字符
imageTTFText($im, $fontSize, $angle, $charX,  $charY, $text_color, $font, $strNum[$i]);
}
for($i=0; $i <= 5; $i++) { //循环画背景线
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$linex = mt_rand(1, $width-1);
$liney = mt_rand(1, $height-1);
imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
}
for($i=0; $i <= 32; $i++) { //循环画背景点,生成麻点效果
$pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor);
}
imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor); //画边框
ob_clean();
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
/**
 * 功能:生成随机字符串
 */
exit;
?>
好的,以下是一个简单的 PHP 文件,用于生成和校验 163 邮箱的验证码: ```php <?php session_start(); // 生成随机验证码 function generateCode() { $code = ""; $length = 4; // 验证码长度 $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ($i = 0; $i < $length; $i++) { $code .= $chars[mt_rand(0, strlen($chars)-1)]; } return $code; } // 发送验证码到邮箱 function sendCode($email, $code) { $subject = "验证码"; $message = "您的验证码为:$code"; $headers = "From: example@example.com"; // 替换成您的发件邮箱 return mail($email, $subject, $message, $headers); } // 校验验证码 function verifyCode($code) { return isset($_SESSION['code']) && strtolower($code) == strtolower($_SESSION['code']); } // 处理请求 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $email = $_POST['email']; $action = $_POST['action']; if ($action == 'send') { $code = generateCode(); $_SESSION['code'] = $code; if (sendCode($email, $code)) { echo "验证码已发送,请查收邮件。"; } else { echo "验证码发送失败,请稍后再试。"; } } else if ($action == 'verify') { $code = $_POST['code']; if (verifyCode($code)) { echo "验证码正确。"; unset($_SESSION['code']); } else { echo "验证码错误。"; } } } ?> ``` 使用方法: 1. 在需要使用验证码的页面中,引入此文件; 2. 在需要发送验证码的地方,向此文件发送 POST 请求,参数包括邮箱和 `action`(值为 `send`); 3. 在需要校验验证码的地方,向此文件发送 POST 请求,参数包括邮箱、验证码和 `action`(值为 `verify`); 4. 根据返回结果进行相应的处理。 需要注意的是,此代码仅供参考,还需要根据实际需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值