首页 > 编程知识 正文

php生成随机验证码下载(PHP生成验证码)

时间:2023-12-05 14:04:50 阅读:312152 作者:EDJE

本文目录一览:

  • 1、php如何实现邮箱验证
  • 2、如何用PHP生成验证码
  • 3、php中如何使用随机函数rand()生成一个数字验证码
  • 4、怎样用PHP制作验证码

php如何实现邮箱验证

这是一个可以发送邮件的程序。程序是直接使用SMTP协议进行发送,用到了经典的phpMailer库。

请阅读apply.php文件。

发邮件之前你需要一个用来发送邮件的邮箱账号。但不能使用这个账号发送太多的邮件,否则会被当做垃圾邮件屏蔽。如果发送量较大,可以多申请几个邮箱。

关键变量说明:

subject: 邮件的主题

email: 邮件的内容

host: SMTP主机,与你申请的邮箱的服务商有关,详见代码内注释

fromname: 发件人的名字,可以任意写,对方收到时能看到这个名字

from: 发件人地址

to: 收件人地址,代码中写了$to = $from,请自行更改

username和password: 你邮箱的账号和密码,一般username都等于发件人地址。必须提供,否则无法发送邮件。

PHPMailer说明:

$mailer = new PHPMailer(true);

$mailer-IsHTML(true); // 这是一封HTML邮件

$mailer-IsSMTP(true); // 连接SMTP服务发送邮件

# $mailer-SMTPDebug = true; // 是否开启调试模式

$mailer-CharSet = 'UTF-8'; // 邮件内容的编码,和你程序的编码保持一致

$mailer-Encoding = 'base64'; // 邮件传递过程使用的编码

$mailer-FromName = $fromname; // 发件人

$mailer-Host = $host; // SMTP服务地址

$mailer-AddAddress($to); // 添加收件人

$mailer-From = $from; // 设置发件人

$mailer-Subject = $subject; // 设置主题

$mailer-MsgHTML($email); // 设置HTML邮件内容

$mailer-SMTPAuth = true; // 开启SMTP验证

$mailer-Username = $username; // 设置用户名

$mailer-Password = $passwd; // 设置密码

$mailer-Send(); // 发送邮件

如何用PHP生成验证码

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;

2、为图片设置背景色;

3、设置字体颜色和样式;

4、产生4位数的随机的验证码;

5、把产生的每个字符调整旋转角度和位置画到png图片上;

6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;

7、输出图片;

8、释放图片所占内存。

session_start(); 

getCode(4,60,20); 

 

function getCode($num,$w,$h) { 

    $code = ""; 

    for ($i = 0; $i  $num; $i++) { 

        $code .= rand(0, 9); 

    } 

    //4位验证码也可以用rand(1000,9999)直接生成 

    //将生成的验证码写入session,备验证时用 

    $_SESSION["helloweba_num"] = $code; 

    //创建图片,定义颜色值 

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

    $im = imagecreate($w, $h); 

    $black = imagecolorallocate($im, 0, 0, 0); 

    $gray = imagecolorallocate($im, 200, 200, 200); 

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

    //填充背景 

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

 

    //画边框 

    imagerectangle($im, 0, 0, $w-1, $h-1, $black); 

 

    //随机绘制两条虚线,起干扰作用 

    $style = array ($black,$black,$black,$black,$black, 

        $gray,$gray,$gray,$gray,$gray 

    ); 

    imagesetstyle($im, $style); 

    $y1 = rand(0, $h); 

    $y2 = rand(0, $h); 

    $y3 = rand(0, $h); 

    $y4 = rand(0, $h); 

    imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); 

    imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); 

 

    //在画布上随机生成大量黑点,起干扰作用; 

    for ($i = 0; $i  80; $i++) { 

        imagesetpixel($im, rand(0, $w), rand(0, $h), $black); 

    } 

    //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 

    $strx = rand(3, 8); 

    for ($i = 0; $i  $num; $i++) { 

        $strpos = rand(1, 6); 

        imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); 

        $strx += rand(8, 12); 

    } 

    imagepng($im);//输出图片 

    imagedestroy($im);//释放图片所占内存 

}

php中如何使用随机函数rand()生成一个数字验证码

如果要生成四位数字的验证码,则可以用函数:

$srand = rand(1000,9999);

会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最小、最大值。

怎样用PHP制作验证码

?php

//验证码类

class ValidateCode {

 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子

 private $code;//验证码

 private $codelen = 4;//验证码长度

 private $width = 90;//宽度

 private $height = 40;//高度

 private $img;//图形资源句柄

 private $font;//指定的字体

 private $fontsize = 20;//指定字体大小

 private $fontcolor;//指定字体颜色

 //构造方法初始化

 public function __construct() {

  $this-font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片

 }

 //生成随机码

 private function createCode() {

  $_len = strlen($this-charset)-1;

  for ($i=0;$i$this-codelen;$i++) {

   $this-code .= $this-charset[mt_rand(0,$_len)];

  }

 }

 //生成背景

 private function createBg() {

  $this-img = imagecreatetruecolor($this-width, $this-height);

  $color = imagecolorallocate($this-img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));

  imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);

 }

 //生成文字

 private function createFont() {

  $_x = $this-width / $this-codelen;

  for ($i=0;$i$this-codelen;$i++) {

   $this-fontcolor = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

   imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);

  }

 }

 //生成线条、雪花

 private function createLine() {

  //线条

  for ($i=0;$i6;$i++) {

   $color = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

   imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);

  }

  //雪花

  for ($i=0;$i100;$i++) {

   $color = imagecolorallocate($this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

   imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-height),'*',$color);

  }

 }

 //输出

 private function outPut() {

  header('Content-type:image/png');

  imagepng($this-img);

  imagedestroy($this-img);

 }

 //对外生成

 public function doimg() {

  $this-createBg();

  $this-createCode();

  $this-createLine();

  $this-createFont();

  $this-outPut();

 }

 //获取验证码

 public function getCode() {

  return strtolower($this-code);

 }

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。