PHP 驗(yàn)證碼的實(shí)現(xiàn)代碼
更新時(shí)間:2011年07月17日 13:02:35 作者:
PHP 驗(yàn)證碼的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
checkcode.php 生成驗(yàn)證碼圖片,還有變量 $_SESSION[check_pic]。
<?
session_start();
for($i=0; $i<4; $i++){
$rand.= dechex(rand(1,15));
}
$_SESSION[check_pic]=$rand;
//echo $_SESSION[check_pic];
// 設(shè)置圖片大小
$im = imagecreatetruecolor(100,30);
// 設(shè)置顏色
$bg=imagecolorallocate($im,0,0,0);
$te=imagecolorallocate($im,255,255,255);
// 把字符串寫(xiě)在圖像左上角
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
// 輸出圖像
header("Content-type:image/jpeg");
imagejpeg($im);
?>
form.php
通過(guò) <img src="checkcode.php"> 調(diào)用生成的驗(yàn)證碼圖片
<div class="bottomAds">
<fieldset class="bottomAds_quote"><legend>留言</legend>
<div class="ads">
<form action="../utity/post.php" method="post" onsubmit="return chkinput(this)">
<input name="name" type="text" /> 您的名字
<input name="email" type="text" /> 您的郵件
<input name="website" type="text" /> 您的網(wǎng)站
<textarea name="content" style="width:340; height:150;">
</textarea><br />
<img src="checkcode.php"><input type="text" name="check"><br />
<input type="submit" value="提交" />
</form>
</div>
<br clear="both" />
</fieldset>
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te); 使用了 int imagestring(int im, int font, int x, int y, string s, int col); 函數(shù),這個(gè)函數(shù)用于繪橫式字符串。
這個(gè)函數(shù)在圖片上繪出水平的橫式字符串。參數(shù) font 為字形,設(shè)為 1 到 5 表示使用默認(rèn)字形。參數(shù) x、y 為字符串起點(diǎn)坐標(biāo)。字符串的內(nèi)容放在參數(shù) s 上。參數(shù) col 表示字符串的顏色。
post.php
比較 $_POST[check] 與 $_SESSION[check_pic],若相等則執(zhí)行數(shù)據(jù)庫(kù)插入操作。不相等就返回上一頁(yè)。
<?php
session_start();
if(isset($_POST[check]))
{
if($_POST[check] == $_SESSION[check_pic])
{
// echo "驗(yàn)證碼正確".$_SESSION[check_pic];
require("dbinfo.php");
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$content = $_POST['content'];
$date = date("Y-m-d h:m:s");
// 連接到 MySQL 服務(wù)器
$connection = mysql_connect ($host, $username, $password);
if (!$connection)
{
die('Not connected : ' . mysql_error());
}
// 設(shè)置活動(dòng)的 MySQL 數(shù)據(jù)庫(kù)
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die ('Can\'t use db : ' . mysql_error());
}
// 向數(shù)據(jù)庫(kù)插入數(shù)據(jù)
$query = "insert into table (nowamagic_name, nowamagic_email, nowamagic_website, nowamagic_content, nowamagic_date) values ('$name','$email','$website','$content','$date')";
$result = mysql_query($query);
if($result)
{
echo "<script>alert('提交成功'); history.go(-1);</script>";
}
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}
else
{
echo "<script>alert('驗(yàn)證碼錯(cuò)誤'); history.go(-1);</script>";
}
}
?>
復(fù)制代碼 代碼如下:
<?
session_start();
for($i=0; $i<4; $i++){
$rand.= dechex(rand(1,15));
}
$_SESSION[check_pic]=$rand;
//echo $_SESSION[check_pic];
// 設(shè)置圖片大小
$im = imagecreatetruecolor(100,30);
// 設(shè)置顏色
$bg=imagecolorallocate($im,0,0,0);
$te=imagecolorallocate($im,255,255,255);
// 把字符串寫(xiě)在圖像左上角
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
// 輸出圖像
header("Content-type:image/jpeg");
imagejpeg($im);
?>
form.php
通過(guò) <img src="checkcode.php"> 調(diào)用生成的驗(yàn)證碼圖片
復(fù)制代碼 代碼如下:
<div class="bottomAds">
<fieldset class="bottomAds_quote"><legend>留言</legend>
<div class="ads">
<form action="../utity/post.php" method="post" onsubmit="return chkinput(this)">
<input name="name" type="text" /> 您的名字
<input name="email" type="text" /> 您的郵件
<input name="website" type="text" /> 您的網(wǎng)站
<textarea name="content" style="width:340; height:150;">
</textarea><br />
<img src="checkcode.php"><input type="text" name="check"><br />
<input type="submit" value="提交" />
</form>
</div>
<br clear="both" />
</fieldset>
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te); 使用了 int imagestring(int im, int font, int x, int y, string s, int col); 函數(shù),這個(gè)函數(shù)用于繪橫式字符串。
這個(gè)函數(shù)在圖片上繪出水平的橫式字符串。參數(shù) font 為字形,設(shè)為 1 到 5 表示使用默認(rèn)字形。參數(shù) x、y 為字符串起點(diǎn)坐標(biāo)。字符串的內(nèi)容放在參數(shù) s 上。參數(shù) col 表示字符串的顏色。
post.php
比較 $_POST[check] 與 $_SESSION[check_pic],若相等則執(zhí)行數(shù)據(jù)庫(kù)插入操作。不相等就返回上一頁(yè)。
復(fù)制代碼 代碼如下:
<?php
session_start();
if(isset($_POST[check]))
{
if($_POST[check] == $_SESSION[check_pic])
{
// echo "驗(yàn)證碼正確".$_SESSION[check_pic];
require("dbinfo.php");
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$content = $_POST['content'];
$date = date("Y-m-d h:m:s");
// 連接到 MySQL 服務(wù)器
$connection = mysql_connect ($host, $username, $password);
if (!$connection)
{
die('Not connected : ' . mysql_error());
}
// 設(shè)置活動(dòng)的 MySQL 數(shù)據(jù)庫(kù)
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die ('Can\'t use db : ' . mysql_error());
}
// 向數(shù)據(jù)庫(kù)插入數(shù)據(jù)
$query = "insert into table (nowamagic_name, nowamagic_email, nowamagic_website, nowamagic_content, nowamagic_date) values ('$name','$email','$website','$content','$date')";
$result = mysql_query($query);
if($result)
{
echo "<script>alert('提交成功'); history.go(-1);</script>";
}
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}
else
{
echo "<script>alert('驗(yàn)證碼錯(cuò)誤'); history.go(-1);</script>";
}
}
?>
您可能感興趣的文章:
- 一個(gè)漂亮的php驗(yàn)證碼類(lèi)(分享)
- php圖片驗(yàn)證碼代碼
- PHP驗(yàn)證碼類(lèi)代碼( 最新修改,完全定制化! )
- php中文字母數(shù)字驗(yàn)證碼實(shí)現(xiàn)代碼
- ThinkPHP驗(yàn)證碼使用簡(jiǎn)明教程
- PHP中文漢字驗(yàn)證碼
- php 生成隨機(jī)驗(yàn)證碼圖片代碼
- PHP驗(yàn)證碼函數(shù)代碼(簡(jiǎn)單實(shí)用)
- php生成圖形驗(yàn)證碼幾種方法小結(jié)
- 一個(gè)經(jīng)典的PHP驗(yàn)證碼類(lèi)分享
- 一個(gè)好用的PHP驗(yàn)證碼類(lèi)實(shí)例分享
- PHP生成圖像驗(yàn)證碼的方法小結(jié)(2種方法)
相關(guān)文章
WordPress中用于創(chuàng)建以及獲取側(cè)邊欄的PHP函數(shù)講解
這篇文章主要介紹了WordPress中用于創(chuàng)建以及獲取側(cè)邊欄的PHP函數(shù)講解,分別為register_sidebar()函數(shù)和get_sidebar()的使用,需要的朋友可以參考下2015-12-12php設(shè)計(jì)模式之簡(jiǎn)單工廠模式詳解
這篇文章主要介紹了php設(shè)計(jì)模式的簡(jiǎn)單工廠模式,又稱(chēng)為靜態(tài)工廠方法模式,是一種重要的PHP設(shè)計(jì)模式,需要的朋友可以參考下2014-09-09php將一維數(shù)組轉(zhuǎn)換為每3個(gè)連續(xù)值組成的二維數(shù)組
這篇文章主要介紹了php將一維數(shù)組轉(zhuǎn)換為每3個(gè)連續(xù)值組成的二維數(shù)組的方法,涉及array_slice函數(shù)的使用技巧,需要的朋友可以參考下2016-05-05php使用fsockopen函數(shù)發(fā)送post,get請(qǐng)求獲取網(wǎng)頁(yè)內(nèi)容的方法
這篇文章主要介紹了php使用fsockopen函數(shù)發(fā)送post,get請(qǐng)求獲取網(wǎng)頁(yè)內(nèi)容的方法,是PHP關(guān)于socket編程的一個(gè)典型應(yīng)用,需要的朋友可以參考下2014-11-11PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來(lái)為您詳細(xì)解答2013-01-01PHP中強(qiáng)制類(lèi)型轉(zhuǎn)換的示例詳解
這篇文章主要給大家介紹了關(guān)于PHP中強(qiáng)制類(lèi)型轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01php5 pdo新改動(dòng)加載注意事項(xiàng)
想試試pdo怎么用,把 extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll2008-09-09