checkcode.php 核心頁面
<?php
session_start();
$checkcode="";
for($i=1;$i<=4;$i++){
$checkcode.=dechex(rand(1,15));
}
$_SESSION['checkcode']=$checkcode;
//創建畫布
$image=imagecreatetruecolor(80,30);
//設置一個顏色
$color=imagecolorallocate($image,255,255,255);
//寫入畫布
imagestring($image,rand(2,5),rand(0,50),rand(0,20),$checkcode,$color);
//干擾線
for($i=0;$i<10;$i++){
imageline($image,rand(0,80),rand(0,30),rand(0,80),rand(0,30),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
//輸出
header( "Content-type: image/png" );
imagepng ( $image );
?>
login.php 視圖頁面
<form action="loginProcess.php" method="post">
驗證碼:<input type="text" name="checkcode"> <img src="checkcode.php" οnclick="this.src='checkcode.php?a=Math.random()'">
</form>
loginProcess.php 邏輯頁面
<?php
session_start();
if($_POST['checkcode']==$_SESSION['checkcode']){
echo "驗證碼正確";
}else{
echo "驗證碼錯誤";
}
?>