A. php 繪制網站登錄首頁圖片驗證碼
幾乎所有的網站登錄頁都會有驗證碼,驗證碼是一種安全保護機制,在注冊時要求必須有人工操作進行驗證,用於防止垃圾注冊機大量注冊用戶賬號佔用伺服器內存從而使伺服器癱瘓。
圖片驗證碼的實現十分簡單。首先從指定字元集合中隨機抽取固定數目的字元,以一種不規則的方法畫在畫布上,再適當添加一些干擾點和干擾元素,最後將圖片輸出,一張嶄新的驗證碼就完成了。
先給大家展示下生成的驗證碼:
點擊刷新:
如果大家對實現效果非常滿意,請繼續往下看。
前端代碼如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="賬號"/><br/>
<input
type="password"
placeholder="密碼"/><br/>
<input
type="text"
placeholder="驗證碼"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">點擊刷新</a><br/>
<input
type="submit"
value="登錄"/>
</form>
<script
type="text/javascript">
//刷新驗證碼
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php腳本文件驗證碼的代碼如下:
<?php
session_start();
//開啟session記錄驗證碼數據
vCode(4,
15);//設置驗證碼的字元個數和圖片基礎寬度
//vCode
字元數目,字體大小,圖片寬度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//設置驗證碼字元集合
$str
=
"";
//保存獲取的驗證碼
$code
=
''
//隨機選取字元
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//創建驗證碼畫布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
畫干擾線
for($i
=
0;$i
<
5;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagearc($im,
mt_rand(-
$width,
$width),
mt_rand(-
$height,
$height),
mt_rand(30,
$width
*
2),
mt_rand(20,
$height
*
2),
mt_rand(0,
360),
mt_rand(0,
360),
$font_color);
}
//
畫干擾點
for($i
=
0;$i
<
50;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagesetpixel($im,
mt_rand(0,
$width),
mt_rand(0,
$height),
$font_color);
}
//隨機旋轉角度數組
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
輸出驗證碼
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次請求時都會訪問伺服器
//max-age在請求1s後再次請求會再次訪問伺服器,must-revalidate則第一發送請求會訪問伺服器,之後不會再訪問伺服器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//將圖片轉化為png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,關於小編給大家介紹的php繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!
B. 請教網頁製作中PHP驗證碼實現問題。
在文件1用表單提交給文件2,輸入驗證碼肯定是在表單的輸入框里,輸入好後提交表單時對象是文件2就行了。注意php需要伺服器支持啊,沒伺服器直接當文本打開了。
C. 怎麼用PHP設置密碼和驗證碼
驗證碼可以用緩存隨機數字
密碼驗證直接讀取緩存就可以
D. 如何實現php手機簡訊驗證功能
現在網站在建設網站時為了保證用戶信息的真實性,往往會選擇發簡訊給用戶手機發驗證碼信息,只有通過驗證的用戶才可以注冊,這樣保證了用戶的聯系信息資料的100%的准確性。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html xmlns>
<head>
<title></title>
<script src="js/jquery-1.4a2.min.js" type="text/javascript"></script>
<script type="text/javascript">
/*-------------------------------------------*/
var InterValObj; //timer變數,控制時間
var count = 60; //間隔函數,1秒執行
var curCount;//當前剩餘秒數
var code = ""; //驗證碼
var codeLength = 6;//驗證碼長度
function sendMessage() {
curCount = count;
var dealType; //驗證方式
tel = $(』#tel』).val();
if(tel!=』』){
//驗證手機有效性
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+d{8})$/;
if(!myreg.test($(』#tel』).val()))
{
alert(』請輸入有效的手機號碼!』);
return false;
}
tel = $(』#tel』).val();
//產生驗證碼
for (var i = 0; i < codeLength; i++) {
code += parseInt(Math.random() * 9).toString();
}
//設置button效果,開始計時
$("#btnSendCode").attr("disabled", "true");
$("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");
InterValObj = window.setInterval(SetRemainTime, 1000); //啟動計時器,1秒執行一次
//向後台發送處理數據
$.ajax({
type: "POST", //用POST方式傳輸
dataType: "text", //數據格式:JSON
url: 』yanzhengma.php』, //目標地址(根據實際地址)
data: "&tel=" + tel + "&code=" + code,
error: function (XMLHttpRequest, textStatus, errorThrown) { },
success: function (msg){ }
});
}else{
alert(』請填寫手機號碼』);
}
}
//timer處理函數
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止計時器
$("#btnSendCode").removeAttr("disabled");//啟用按鈕
$("#btnSendCode").val("重新發送驗證碼");
code = ""; //清除驗證碼。如果不清除,過時間後,輸入收到的驗證碼依然有效
}
else {
curCount--;
$("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");
}
}
</script>
</head>
<body>
<input name="tel" id=tel type="text" />
<input id="btnSendCode" type="button" value="發送驗證碼" onclick="sendMessage()" /></p>
</body>
</html>
第三、調用簡訊伺服器簡訊介面
整理的頁面是yanzhengma.php(具體根據服務商提供信息)
<?php //提交簡訊
$post_data = array();
$post_data[』userid』] =簡訊服務商提供ID;
$post_data[』account』] = 』簡訊服務商提供用戶名』;
$post_data[』password』] = 』簡訊服務商提供密碼』;
// Session保存路徑
$sessSavePath = dirname(__FILE__)."/../data/sessions/";
if(is_writeable($sessSavePath) && is_readable($sessSavePath)){
session_save_path($sessSavePath);
}
session_register(』mobliecode』);
$_SESSION[』mobilecode』] = $_POST["code"];
$content=』簡訊驗證碼:』.$_POST["code"].』【簡訊驗證】』;
$post_data[』content』] = mb_convert_encoding($content,』utf-8』, 』gb2312』); //簡訊內容需要用urlencode編碼下
$post_data[』mobile』] = $_POST["tel"];
$post_data[』sendtime』] = 』』; //不定時發送,值為0,定時發送,輸入格式YYYYMMDDHHmmss的日期值
$url=』http://IP:8888/sms.aspx?action=send』;
$o=』』;
foreach ($post_data as $k=>$v)
{
$o.="$k=".$v.』&』;
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要將結果直接返回到變數里,那加上這句。
$result = curl_exec($ch);
?>
第四:提交表單信息時對簡訊驗證碼驗證
//手機驗證碼開始
session_start();
$svalitel = $_SESSION[』mobilecode』];
$vdcodetel = empty($vdcodetel) ? 』』 : strtolower(trim($vdcodetel));
if(strtolower($vdcodetel)!=$svalitel || $svalitel==』』)
{
ResetVdValue();
//echo "Pageviews=".$vdcodetel;
ShowMsg("手機驗證碼錯誤!", 』-1』);
exit();
}
E. php驗證碼怎麼使用
調用你這個代碼里已經生成的驗證碼,我給你的是用戶輸入時候的頁面,你還要再寫一個驗證的頁,就是驗證用戶輸入的和代碼生成的是否一樣 <form action="**.php" method="post"> <table> <tr> <td width="490" align="left"> <input size="8" name="$vcode" /> <img src="./verifyImg.php"(比如說這個是你的文件的文件名) onClick="this.src='./verifyImg.php?=random'+Math.random();" style="cursor:pointer;"> <span>點擊切換圖片</span><br /> <input type="submit" value="提交" /> </td></tr></table>
F. php 驗證碼類需要開啟哪些拓展
你好,php驗證碼類只需要開啟gd2庫就可以了。
G. 驗證碼怎麼用php實現
<?php
/*
* Filename: authpage.php
*/
srand((double)microtime()*1000000);
//驗證用戶輸入是否和驗證碼一致
if(isset($HTTP_POST_VARS['authinput']))
{
if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)
echo "驗證成功!";
else
echo "驗證失敗!";
}
//生成新的四位整數驗證碼
while(($authnum=rand()%10000)<1000);
?>
<form action=authpage.php method=post>
<table>
請輸入驗證碼:<input type=text name=authinput style="width:
80px"><br>
<input type=submit name="驗證" value="提交驗證碼">
<input type=hidden name=authnum value=<? echo $authnum; ?>>
<img src=authimg.php?authnum=<? echo $authnum; ?>>
</table>
</form>
代碼二:
<?php
/*
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/
//生成驗證碼圖片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);
for($i=0;$i<50;$i++) //加入干擾象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
ImagePNG($im);
ImageDestroy($im);
?>
H. php怎麼編寫手機簡訊驗證碼功能
以前在遠標做過你的應用應該是這樣吧,用戶輸入手機號碼,點擊發送簡訊,用戶收到驗證碼,輸入對應的驗證碼 判斷是否正確。
需要:
申請一個簡訊介面,就是點擊發送驗證碼的時候,提交到介面給該號碼下發驗證碼。
技術方面的實現:
1、點擊獲取驗證碼
2、程序ajax post提交到簡訊介面
3、簡訊介面服務商 介面判斷用戶和口令,正確後,下發簡訊給該號碼。
4、用戶輸入號碼,程序判斷驗證碼是否一致。
I. php驗證碼怎麼在前from表單驗證呢請大師請教,謝謝
把驗證碼的值當作一個參數存在html表單/ajax表單中(如,以vcode命名增加一個hiden text);
在控制器接收表單的邏輯,接收$_POST['vcode'],判斷$_POST['vcode']是不是生成的驗證碼,是,跳過,不是返回驗證碼錯誤到表單頁面;
其實驗證就類似介面一樣,雙方約定規則,統一密鑰,而驗證只是你自己去驗證罷了
J. 我的php代碼中登陸界面加一個驗證碼,如何實現
php登陸頁面+驗證碼的實現,參考如下:
1、首先新建一個php站點;