php驗證碼代碼
A. php的圖片驗證碼代碼
這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
<?php
session_start();
$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理緩沖
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //顯示4個字元
$imageX = strlen($radomstring)*8; //圖像的寬
$imageY = 20; //圖像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色並說明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2
//與左上角的顏色相同的都會被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往圖像上寫入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);
//畫邊框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
//畫一些隨機出現的點
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//畫隨機出現的線
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('請檢查文件目錄完整性:/images/checkcode/');
$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //獲得文件名
header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>
B. PHP驗證碼代碼問題
<form action="" method="post">
<img src='2.php'><br> 「 這句調用的文件對不,是你第一個文件不?你把這個 2.php換成你驗證碼的那個文件試試
<input type="text" name="check" ><br>
<input type="submit" value="提交" ><br>
</form>
C. php中驗證賬號密碼的代碼
<?php
session_start();
if($_POST['name']&&$_POST['pass'])
{
$name=$_POST['name']; //接受傳遞值
$pass=$_POST['pass'];
}
else
{
echo '用戶名或密碼不能為空';//當然也可以進行其他操作,比如使用header('Location:error.php')來把用戶重定向到一個錯誤提示頁
exit;
}
include_once('mysql_connect.php');//連接資料庫
$name=mysql_real_escape_string($name); //過濾信息
$pass=mysql_real_escape_string($pass);
$sql='select username,userpass from userdata where username="$name"';//從資料庫中取出用戶信息
$r=mysql_query($sql);
if(mysql_num_rows==0)
{
echo '用戶名不存在';
exit;
}
else
{
$row=mysql_fetch_assoc($r);
if($pass!=$row['userpass'])
{
echo '用戶密碼錯誤';
exit;
}
esle
{
$_SESSION['user']=$name; //創建SESSION
}
}
?>
D. php驗證碼怎麼使用
把以上代碼保存成code.php,上傳到相應目錄,如網站根目錄 調用的時候 <img src="code.php"> 即可
E. 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繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!
F. 驗證碼怎麼用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);
?>
G. 求一個php登錄代碼,有驗證碼功能,能答必採納。
<?php
Header("Content-type: image/gif");
$border = 1;
$how = 4;
$w = $how*15;
$h = 20;
$fontsize = 10;
$alpha = "abcdefghijkmnopqrstuvwxyz";
$number = "023456789";
$randcode = "";
srand((double)microtime()*1000000);
$im = ImageCreate($w, $h);
$bgcolor = ImageColorAllocate($im, 255, 255, 255);
ImageFill($im, 0, 0, $bgcolor);
if($border)
{
$black = ImageColorAllocate($im, 0, 0, 0);
ImageRectangle($im, 0, 0, $w-1, $h-1, $black);
}
for($i=0; $i<$how; $i++)
{
$alpha_or_number = mt_rand(0, 1);
$str = $alpha_or_number ? $alpha : $number;
$which = mt_rand(0, strlen($str)-1);
$code = substr($str, $which, 1);
$j = !$i ? 4 : $j+15;
$color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
ImageChar($im, $fontsize, $j, 3, $code, $color3);
$randcode .= $code;
}
for($i=0; $i<5; $i++)
{
$color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1);
}
for($i=0; $i<$how*40; $i++)
{
$color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2);
}
session_start();
$_SESSION['randcode'] = $randcode;
Imagegif($im);
ImageDestroy($im);
?>
以上是隨機生成驗證碼的代碼,加入保存為yz.php,則使用時只需,<img src="yz.php">
<?php
session_start();
if($_SESSION['randcode'] = $_POST['yzm']){
echo '驗證碼錯誤'
exit;
}else{
$sql = "SELECT id FROM table WHERE yhm = '".$_POST['yhm']."' AND mm = '".$_POST['mm']."'";
$result = mysql_query($sql);
if($result){
echo '<meta http-equiv="refresh" content="0;url=index.html"/>'
exit;
}else{
echo '賬號或密碼錯誤'
exit;
}
}
?>
比較簡單,懂的應該都懂
H. 我的php代碼中登陸界面加一個驗證碼,如何實現
php登陸頁面+驗證碼的實現,參考如下:
1、首先新建一個php站點;
I. php 登陸驗證碼通不過
<?php
session_start();
$CheckCode=@$_POST['CheckCode'];
$chak=@$_POST['yes'];
if($chak != "ok"){
?> <form action="abc.php" method="post" >
驗證碼:<input type='text' name='CheckCode' /><img src='換成你的這個產生驗證碼代碼頁面' />
<input type="hidden" name="yes" value="ok">
<input type="submit" value="提交">
</form>
<?php
}else{
if(strtolower($CheckCode)!=$_SESSION['authnum']){
echo "<script>alert('校驗碼不正確!');window.location.href='abc.php';</script>";
exit;
}else{
echo "對的checkcode=".$CheckCode."相等於session(authum)".$_SESSION['authnum'];
echo "<script>alert('校驗碼正確!');window.location.href='bcd.php';</script>";
}
}//bcd.php這個什麼都沒有就是一個空頁,你試一下。
?>
J. php中用戶輸入的驗證碼怎樣用javascript 代碼進行判斷 是否輸入正確
驗證碼一般都是在伺服器隨機產生,在html頁面用ajax獲取驗證碼來和用戶輸入的驗證碼進行比較
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function doit()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("get","auth_code.php",true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200 || xmlHttp.status==0)
{
checkAuthCodeInput(xmlHttp.responseText);
}
}
}
function checkAuthCodeInput(authcode)
{
if(document.getElementById("自定義").value==auth)
{
return true;
}
return false;
}
</script>
<img src="auth_code.php" id="img_auth_code"/>
<a href="document.getElementById('img_auth_code').src='auth_code.php?code='+Math.random();">看不清,再換一張</a>
//該頁面用於隨機獲取驗證碼rand_auth_code.php
<?php
function get_auth_code()
{
for($i=0;$i<5;$i++)
{
$_GLOBALS['rand_str'].=strtoupper(dechex(rand(0,15)));
rand_str_width+=imagefontwidth($i);
}
}
echo $_GLOBALS['rand_str'];
?>
<?php
include_once("rand_auth_code.php");//導入產生驗證碼的頁面
$img_width=100;
$img_height=25;
$img=imagecreatetruecolor($img_width,$img_height);
$img_bg_color=imagecolorallocate($img,0,0,0);
imagefill($img,0,0,$img_bg_color);
$img_font_color=imagecolorallocate($img,225,225,225);
get_auth_code();//調用方法,產生隨機驗證碼
$img_font_x=$img_width/2 - $rand_str_width/2;
$img_font_y=($img_height-imagefontwidth(5))/2;
imagestring($img,5,$img_font_x,$img_font_y,$_GLOBALS[rand_str],$img_font_color);
header("Content-Type:image/jpeg");
imagejpeg($img);
imagedestroy($img);
unset($_GLOBALS['rand_str']);
?>
不知是否符合你的要求