PHP 驗證碼的實現(xiàn)代碼
更新時間:2011年07月17日 13:02:35 作者:
PHP 驗證碼的實現(xiàn)代碼,需要的朋友可以參考下。
checkcode.php 生成驗證碼圖片,還有變量 $_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);
// 把字符串寫在圖像左上角
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
// 輸出圖像
header("Content-type:image/jpeg");
imagejpeg($im);
?>
form.php
通過 <img src="checkcode.php"> 調(diào)用生成的驗證碼圖片
<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ù),這個函數(shù)用于繪橫式字符串。
這個函數(shù)在圖片上繪出水平的橫式字符串。參數(shù) font 為字形,設(shè)為 1 到 5 表示使用默認字形。參數(shù) x、y 為字符串起點坐標。字符串的內(nèi)容放在參數(shù) s 上。參數(shù) col 表示字符串的顏色。
post.php
比較 $_POST[check] 與 $_SESSION[check_pic],若相等則執(zhí)行數(shù)據(jù)庫插入操作。不相等就返回上一頁。
<?php
session_start();
if(isset($_POST[check]))
{
if($_POST[check] == $_SESSION[check_pic])
{
// echo "驗證碼正確".$_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 服務器
$connection = mysql_connect ($host, $username, $password);
if (!$connection)
{
die('Not connected : ' . mysql_error());
}
// 設(shè)置活動的 MySQL 數(shù)據(jù)庫
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die ('Can\'t use db : ' . mysql_error());
}
// 向數(shù)據(jù)庫插入數(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('驗證碼錯誤'); history.go(-1);</script>";
}
}
?>
復制代碼 代碼如下:
<?
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);
// 把字符串寫在圖像左上角
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
// 輸出圖像
header("Content-type:image/jpeg");
imagejpeg($im);
?>
form.php
通過 <img src="checkcode.php"> 調(diào)用生成的驗證碼圖片
復制代碼 代碼如下:
<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ù),這個函數(shù)用于繪橫式字符串。
這個函數(shù)在圖片上繪出水平的橫式字符串。參數(shù) font 為字形,設(shè)為 1 到 5 表示使用默認字形。參數(shù) x、y 為字符串起點坐標。字符串的內(nèi)容放在參數(shù) s 上。參數(shù) col 表示字符串的顏色。
post.php
比較 $_POST[check] 與 $_SESSION[check_pic],若相等則執(zhí)行數(shù)據(jù)庫插入操作。不相等就返回上一頁。
復制代碼 代碼如下:
<?php
session_start();
if(isset($_POST[check]))
{
if($_POST[check] == $_SESSION[check_pic])
{
// echo "驗證碼正確".$_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 服務器
$connection = mysql_connect ($host, $username, $password);
if (!$connection)
{
die('Not connected : ' . mysql_error());
}
// 設(shè)置活動的 MySQL 數(shù)據(jù)庫
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die ('Can\'t use db : ' . mysql_error());
}
// 向數(shù)據(jù)庫插入數(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('驗證碼錯誤'); history.go(-1);</script>";
}
}
?>
相關(guān)文章
PHP正則替換函數(shù)preg_replace和preg_replace_callback使用總結(jié)
這篇文章主要介紹了PHP正則替換函數(shù)preg_replace和preg_replace_callback使用總結(jié),本文是在寫一個模板引擎遇到一個特殊需求時總結(jié)而來,需要的朋友可以參考下2014-09-09
php連接mssql的一些相關(guān)經(jīng)驗及注意事項
php連接mssql對于很多朋友或許已經(jīng)是老生長談,習以為常了,不過本文還是要嘮叨一下,主要以分享連接數(shù)據(jù)庫的相關(guān)經(jīng)驗,感興趣的你可千萬不要錯過,或許本文對你有所幫助呢2013-02-02
php+mysqli事務控制實現(xiàn)銀行轉(zhuǎn)賬實例
這篇文章主要介紹了php+mysqli事務控制實現(xiàn)銀行轉(zhuǎn)賬,實例分析了事物控制的原理與事物回滾的使用技巧,需要的朋友可以參考下2015-01-01
PHP備份數(shù)據(jù)庫生成SQL文件并下載的函數(shù)代碼
這是一個將指定數(shù)據(jù)庫里的所有表備份為一個SQL文件,可下載。這個源碼來自dedecms程序,功能挺多,也很實用,但是代碼的質(zhì)量還有待提高2012-02-02
Windows下利用Gvim寫PHP產(chǎn)生中文亂碼問題解決方法
昨天本來說早點睡覺呢,沒想到一個在寫PHP程序的時候發(fā)現(xiàn)出現(xiàn)的亂碼問題又睡晚了,不過總算解決了。2011-04-04
PHP中iconv函數(shù)轉(zhuǎn)碼時截斷字符問題的解決方法
這篇文章主要介紹了PHP中iconv函數(shù)轉(zhuǎn)碼時截斷字符問題的解決方法,本文給出的解決方法是使用mb_convert_encoding代替iconv,需要的朋友可以參考下2015-01-01

