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 服務(wù)器
$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>";
}
}
?>
復(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);
// 把字符串寫在圖像左上角
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)用生成的驗證碼圖片
復(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ù),這個函數(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ù)庫插入操作。不相等就返回上一頁。
復(fù)制代碼 代碼如下:
<?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 服務(wù)器
$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)文章
WordPress中用于創(chuàng)建以及獲取側(cè)邊欄的PHP函數(shù)講解
這篇文章主要介紹了WordPress中用于創(chuàng)建以及獲取側(cè)邊欄的PHP函數(shù)講解,分別為register_sidebar()函數(shù)和get_sidebar()的使用,需要的朋友可以參考下2015-12-12php將一維數(shù)組轉(zhuǎn)換為每3個連續(xù)值組成的二維數(shù)組
這篇文章主要介紹了php將一維數(shù)組轉(zhuǎn)換為每3個連續(xù)值組成的二維數(shù)組的方法,涉及array_slice函數(shù)的使用技巧,需要的朋友可以參考下2016-05-05php使用fsockopen函數(shù)發(fā)送post,get請求獲取網(wǎng)頁內(nèi)容的方法
這篇文章主要介紹了php使用fsockopen函數(shù)發(fā)送post,get請求獲取網(wǎng)頁內(nèi)容的方法,是PHP關(guān)于socket編程的一個典型應(yīng)用,需要的朋友可以參考下2014-11-11PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細解答2013-01-01