php写简单的随机验证码
生活随笔
收集整理的這篇文章主要介紹了
php写简单的随机验证码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?跟著教程寫隨機(jī)的數(shù)字驗(yàn)證碼?
<?php session_start(); //創(chuàng)建隨機(jī)碼 $_nmsg = ''; for($i = 0; $i<4; $i++){$_nmsg .= dechex(mt_rand(0,15)); } //保存在session $_SESSION['code'] = $_nmsg;//創(chuàng)建一個(gè)真彩色圖像 $_width =75; $_height = 25; $_img = imagecreatetruecolor($_width, $_height); //白色 $_white = imagecolorallocate($_img, 255, 255, 255); //填充 imagefill($_img, 0, 0, $_white); //隨機(jī)畫出6個(gè)線條 for($i = 0; $i<6; $i++){$_md_color = imagecolorallocate($_img,mt_rand(0, 255),mt_rand(0, 255), mt_rand(0, 255));imageline($_img, mt_rand(0, 75), mt_rand(0, 75), mt_rand(0, 75), mt_rand(0, 75),$_md_color); } //隨機(jī)雪花 for($i = 0; $i<100;$i++){$_md_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));imagestring($_img, 1, mt_rand(1, $_width), mt_rand(1, $_height), '*', $_md_color); } //黑色邊框 $_black = imagecolorallocate($_img, 0, 0, 0); imagerectangle($_img, 0, 0, $_width-1, $_height-1, $_black); //輸出驗(yàn)證碼 for($i = 0;$i<strlen($_SESSION['code']);$i++){$_mt_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));imagestring($_img, mt_rand(3, 5), $i*$_width/4+mt_rand(1, 10), mt_rand(1, $_height/2), $_SESSION['code'][$i],$_mt_color); } header('Content-Type:image/png'); imagepng($_img); //銷毀 imagedestroy($_img);?>?
轉(zhuǎn)載于:https://www.cnblogs.com/zhidong123/archive/2012/11/02/2751908.html
總結(jié)
以上是生活随笔為你收集整理的php写简单的随机验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios学习--TableView详细解释
- 下一篇: nfs文件服务器以及客户端基本配置