導航:首頁 > 編程語言 > php算術驗證碼

php算術驗證碼

發布時間:2022-06-23 10:52:07

1. php 怎麼獲取驗證碼的值

$_SESSION["vcode"] = $vcode;
驗證碼的值就在 $_SESSION["vcode"] 裡面。

2. 求大神:網頁驗證碼問題,JS或PHP人士請看:如何讓驗證碼錯誤提示後用戶名和密碼記錄仍在

把<div...>後台管理</div>標簽下面的js腳本換成我下面改的那樣,同時把表單里的onsubmit="return Checklogin();" 這個屬性刪掉,試試看:
<script>
$(function(){
//算術驗證
$("#getcode_math").click(function(){
$(this).attr("src",'code_math.php?' + Math.random());
});

$("#chk_math").click(function(){
if (loginform.user.value=="") { //有個疑問,loginform.user.value能拿到輸入的用戶名嗎?
alert("請填寫登錄名");
loginform.user.focus();
return false;
}
if (loginform.pw.value=="") { //還有這樣,能拿到輸入的密碼?
alert("密碼不能為空");
loginform.pw.focus();
return false;
}

var code_math = $("#code_math").val();
var XMLHttpRequest = $.post("chk_code.php?act=math",{code:code_math},function(msg){
if(msg==0){
alert("驗證碼錯誤!");
}
});
if (XMLHttpRequest.responseText == 0) {
return false;
}
});
});
</script>

3. 請教網頁製作中PHP驗證碼實現問題。

在文件1用表單提交給文件2,輸入驗證碼肯定是在表單的輸入框里,輸入好後提交表單時對象是文件2就行了。注意php需要伺服器支持啊,沒伺服器直接當文本打開了。

4. 怎麼用原生php做驗證碼

<?php
class validateCode{
private $charset=''; //隨機因子
private $_len;
private $code; //驗證碼
private $codelen=4; //驗證碼長度
private $width=130; //畫布寬度
private $height=50; //畫布長度
private $img; //圖形資源句柄
private $font; //制定字體
private $fontsize=26;//字體大小
private $fontcolor; //字體顏色

//初始化構造函數
public function __construct(){
$this->font=dirname(__file__).'/validateFont.ttf';
//字體文件及路徑,__file__表示當前PHP文件絕對路徑,dirname表示這個PHP文件的絕對路徑的上一層
}

//生成隨機碼
private function createCode(){
$this->_len=strlen($this->charset)-1; //strlen表示計算字元串長度
for($i=1;$i<=$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$this->_len)];
}
}
//生成背景
private function createBG(){
$this->img=imagecreatetruecolor($this->width,$this->height);//創建圖像
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); //分配顏色函數(紅,綠,藍)
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); //矩形區域著色函數(要填充的圖像,x1,y1,x2,y2,要填充的顏色),x1,y1,x2,y2表示矩形的對角線
}

//生成文字
private function createFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); //參數 size 為字形的尺寸;angle 為字型的角度,順時針計算,0 度為水平,也就是三點鍾的方向 (由左到右),90 度則為由下到上的文字;x,y 二參數為文字的坐標值 (原點為左上角);參數 col 為字的顏色;fontfile 為字型文件名稱,亦可是遠端的文件;text 當然就是字元串內容了。
}
}

//生成線條雪花
private function createLine(){
//線條
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}

//雪花
for($i=0;$i<40;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}

//輸出
private function outPut(){
header("content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}

//對外生成
public function doimg(){
$this->createCode();
$this->createBg();
$this->createFont();
$this->createLine();
$this->outPut();
}

//獲取驗證碼
public function getCode(){
return strtolower($this->code);
}
}
?>
字體自己去找,修改下路徑就行了

5. php驗證碼怎麼使用

調用你這個代碼里已經生成的驗證碼,我給你的是用戶輸入時候的頁面,你還要再寫一個驗證的頁,就是驗證用戶輸入的和代碼生成的是否一樣 <form action="**.php" method="post"> <table> <tr> <td width="490" align="left"> <input size="8" name="$vcode" /> <img src="./verifyImg.php"(比如說這個是你的文件的文件名) onClick="this.src='./verifyImg.php?=random'+Math.random();" style="cursor:pointer;"> <span>點擊切換圖片</span><br /> <input type="submit" value="提交" /> </td></tr></table>

6. php驗證碼判斷

session_start();
$str_number = trim($_POST['number']);
if(strtolower($_SESSION['rand'])==strtolower($str_number )){
echo "驗證碼正確";
}else{
echo "驗證碼不正確";
}

最好加strtolower函數轉換下大小寫,這樣子,用戶在輸入時就不用區分大小寫了。不然用戶體驗會很麻煩,這是我個人理解。

7. 怎樣用PHP製作驗證碼

<?php

//驗證碼類
classValidateCode{
private$charset='';//隨機因子
private$code;//驗證碼
private$codelen=4;//驗證碼長度
private$width=90;//寬度
private$height=40;//高度
private$img;//圖形資源句柄
private$font;//指定的字體
private$fontsize=20;//指定字體大小
private$fontcolor;//指定字體顏色
//構造方法初始化
publicfunction__construct(){
$this->font=dirname(__FILE__).'/font/elephant.ttf';//注意字體路徑要寫對,否則顯示不了圖片
}
//生成隨機碼
privatefunctioncreateCode(){
$_len=strlen($this->charset)-1;
for($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$_len)];
}
}
//生成背景
privatefunctioncreateBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
privatefunctioncreateFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成線條、雪花
privatefunctioncreateLine(){
//線條
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for($i=0;$i<100;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//輸出
privatefunctionoutPut(){
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//對外生成
publicfunctiondoimg(){
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//獲取驗證碼
publicfunctiongetCode(){
returnstrtolower($this->code);
}
}

8. PHP驗證碼代碼問題

<form action="" method="post">

<img src='2.php'><br> 「 這句調用的文件對不,是你第一個文件不?你把這個 2.php換成你驗證碼的那個文件試試

<input type="text" name="check" ><br>
<input type="submit" value="提交" ><br>

</form>

9. PHP如何生成加減演算法方式的驗證碼

<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加減法驗證類
* 使用示例 VerifyCode::get(1,2);
* 驗證示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php驗證碼
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件頭...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//創建真彩色白紙
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立圖像失敗");
//獲取背景顏色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景顏色
imagefill($im, 0, 0, $background_color);
//獲取邊框顏色
$border_color = imagecolorallocate($im, 200, 200, 200);
//畫矩形,邊框顏色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//獲取隨機淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//畫線
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//設置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//獲取第1個隨機文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//獲取第2個隨機文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//獲取第3個隨機文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//獲取第3個隨機文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//獲取第3個隨機文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//寫入隨機字串
for($i = 0; $i < 5; $i++) {
//獲取隨機較深顏色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//畫文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//顯示圖片
ImagePng($im);
//銷毀圖片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}

10. php 怎麼獲取驗證碼的值,接著怎麼寫

驗證碼一般都保存在session中,列印一下session的值,就能看到驗證碼對應的欄位、值;
mp($_SESSION);

閱讀全文

與php算術驗證碼相關的資料

熱點內容
代碼加密常用方法 瀏覽:950
安卓手機如何解除已禁用 瀏覽:396
演算法的隨機性 瀏覽:485
高中解壓體育游戲 瀏覽:532
androidstudior丟失 瀏覽:345
命令行筆記 瀏覽:737
360目標文件夾訪問拒絕 瀏覽:518
3b編程加工指令 瀏覽:789
c8051f系列單片機選型手冊 瀏覽:772
南昌php程序員 瀏覽:511
bcs命令 瀏覽:446
如何在伺服器指向域名 瀏覽:417
車床編程可以做刀嗎 瀏覽:519
ln命令源碼 瀏覽:791
用粘液做解壓手套 瀏覽:331
icloud收信伺服器地址 瀏覽:500
編程思考者 瀏覽:453
壓縮機型號用什麼氟利昂 瀏覽:553
農機空氣壓縮機 瀏覽:666
程序員下載歌曲 瀏覽:897