- 1、php如何實現郵箱驗證
- 2、如何用PHP生成驗證碼
- 3、php中如何使用隨機函數rand()生成一個數字驗證碼
- 4、怎樣用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的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);//釋放圖片所佔內存
}
如果要生成四位數字的驗證碼,則可以用函數:
$srand = rand(1000,9999);
會生成在1000到9999之間的隨機數字,如果要生成更多位數的數字,可以更改最小、最大值。
?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);
}
}
原創文章,作者:MNEUA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/126198.html