本人初學php,剛剛看到設計驗證碼的部分,自己實踐了一下,自己看代碼看懂,但是自己設計的時候,遇到了一些問題。
本次設計一個簡單的登錄頁面,前臺登錄界面包括:用戶名、密碼、驗證碼以及各個輸入域的簡單驗證(login.php),后臺生成驗證碼(verifycode.php)+驗證碼的驗證(verifycodecheck.php)。
1、登錄頁面及代碼:
登錄界面如圖:

<form id="form1" name="form1" method="post" action="verifycodecheck.php" enctype="multipart/form-data">
<p align="center"><b>用戶登錄</b></p>
<table width="410" border="1" align="center" bgcolor="#00FF99">
<tr>
<td width="100" height="34" align="right">用戶名:<span ></span></td>
<td width="150"><label><input name="username" type="text" id="username" /></label>
</td>
</tr>
<tr>
<td height="36" align="right">密 碼:</td>
<td><label><input name="password" type="password" id="password" /></label>
</td>
</tr>
<tr>
<td height="36" align="right">驗證碼:</td>
<td><label>
<input name="passwordconfirm" type="text" id="passwordconfirm" />
</label>
<label><img src="verifycode.php" name="verifycode" height="25" align="top" <label><a href="javascript:recode()">看不清</a></label>
<input type="hidden" name="defverifycode" />
</td>
</tr>
<tr>
<td height="35" align="right">文件:</td>
<td height="35"><input type="file" name="file" id="file" height="36"/></td>
</tr>
<tr>
<td height="35" colspan="2"><label>
<input type="submit" name="submit" value="提交" NotNullCheck()" /></label>
<label><input type="reset" name="reset" value="重置"/></label></td>
</tr>
</table>
</form>輸入域的驗證代碼如下:
function NotNullCheck() //檢測用戶輸入是否為空
{
var uname=document.getElementById("username").value;
var upassword=document.getElementById("password").value;
var upasswordconfirm=document.getElementById("passwordconfirm").value;
//var uname=document.getElementById("username");
if(uname=="")
{
alert("用戶名不能為空!");
return false;
}
if(upassword=="")
{
alert("密碼不能為空!");
return false;
}
if(upasswordconfirm=="")
{
alert("驗證碼不能為空!");
return false;
}
}點擊圖片或“看不清”鏈接時javascript代碼如下:
function recode()
{
var num1=Math.round(Math.random()*10000000);
var num=num1.toString().substr(0,4);
//alert(num);
form1.verifycode.src="verifycode.php?code="+num; //此處是為在verifycode.php中請求到不同的數據
}2、生成驗證碼(verifycode.php)
<?php
header("Content-type:p_w_picpath/png");
//用GD2庫函數生成圖片驗證碼
$im=p_w_picpathcreate(65,25);
p_w_picpathfill($im,0,0,p_w_picpathcolorallocate($im,200,200,200));
// $verify=$_GET['code'];
$verify="";
$data=array(1,2,3,4,5,6,7,8,9,0);
for($i=0;$i<4;$i++) //產生4為隨機數
{
$verify.=rand(0,9);
}
//
p_w_picpathstring($im,rand(3,5),10,3,substr($verify,0,1),p_w_picpathcolorallocate($im,rand(1,255),0,rand(1,255)));
p_w_picpathstring($im,rand(3,5),25,3,substr($verify,1,1),p_w_picpathcolorallocate($im,0,rand(1,255),rand(1,255)));
p_w_picpathstring($im,rand(3,5),36,3,substr($verify,2,1),p_w_picpathcolorallocate($im,rand(1,255),rand(1,255),0));
p_w_picpathstring($im,rand(3,5),48,3,substr($verify,3,1),p_w_picpathcolorallocate($im,rand(1,255),0,rand(1,255)));
p_w_picpathpng($im);
p_w_picpathdestroy();
session_start();
$_SESSION['code']=$verify; //將生產的驗證碼保存到session['code']中
// echo $_SESSION['code'];
?>3、驗證碼的驗證(verifycodecheck.php)
<?php
session_start();
$verifycode=strtolower($_POST['passwordconfirm']); //轉換為小寫
if($verifycode!=strtolower($_SESSION['code'])) //輸入的驗證碼和圖片驗證碼不一樣
{
echo "<script>alert('verifycode error!');</script>";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='http://localhost:8000/phptest1/02/304/login.php'";
echo "</script>";
}
else
{
echo "<script>alert('WELCOME!');</script>";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='http://localhost:8000/phptest1/02/304/login.php'";
echo "</script>";
}
?>整個設計過程及實現可能還存在問題,希望自己再接再厲...
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。