課程
/后端開發
/PHP
/手把手教你實現電商網站后臺開發
為什么運行源代碼,驗證碼那塊顯示空白
2016-11-14
源自:手把手教你實現電商網站后臺開發 2-1
正在回答
include.php中和image.func.pgp中的session開啟一個就可以了,都開啟就出不來驗證碼
qq_叛屰HR_0
終于知道貼代碼方法了。以下代碼運行在 php?7.0.8 上通過。
include.php
<?php header("content-type:text/html;charset=utf-8"); date_default_timezone_set("PRC"); //session_start(); define("ROOT",?dirname(__FILE__)); set_include_path(PATH_SEPARATOR.ROOT."/lib".PATH_SEPARATOR.ROOT."/core".PATH_SEPARATOR.ROOT."/configs".PATH_SEPARATOR.get_include_path()); //require_once?'mysql.func.php'; require_once?'string.func.php'; require_once?'image.func.php'; /*require_once?'common.func.php'; require_once?'page.func.php'; require_once?'configs.php'; require_once?'admin.inc.php'; require_once?'cate.inc.php'; require_once?'pro.inc.php';*/ //?require_once?'album.inc.php'; //?require_once?'upload.func.php'; //?require_once?'user.inc.php'; //?connect(); ?//?連接數據庫 ?//$link?=?connet();
string.func.php:
<?php? //?產生隨機數 function?buidRandomString($type=1,?$length?=?4){ //?根據參數類型,產生數字、數字小寫字母、數字小寫字母大寫字母等字符 if?($type?==?1)?{ $chars?=?join("",?range(0,?9)); }elseif?($type?==?2)?{ $chars?=?join("",?array_merge(range("a","z"),?range("A",?"Z"))); }elseif?($type?==?3)?{ $chars?=?join("",?array_merge(range("a","z"),?range("A",?"Z"),?range(0,?9))); } if?($length?>?strlen($chars))?{ exit("字符串長度不夠"); } //?隨機地打亂字符串中的所有字符 $chars?=?str_shuffle($chars); //?截取制定長度字符 return?substr($chars,?0,?$length); }
image.func.php :
<?php ? require_once?'../include.php'; /** ?*??通過GD庫創建驗證碼 ?*??$width:?驗證碼圖片寬度 ?*??$height:?驗證碼圖片高度 ?*??$type: 產生字符類型,1為數字,2為數字和小寫字母,3為數字小寫字母加大寫字母 ?*??$length:?驗證碼字符個數 ?*??$pixel:?干擾點數量 ?*??$line:?干擾線數量 ?*??$sess_name:?session?key ?*/ function?verifyImage($type?=?1,?$length?=?4,?$pixel?=?0,?$line?=?0,?$width?=?80,?$height?=?20,?$sess_name?=?"verify"){ //使能session session_start(); //?創建畫布 //?創建真色彩畫布 $image?=?imagecreatetruecolor($width,?$height); //?畫筆顏色 $white?=?imagecolorallocate($image,?255,?255,?255); $black?=?imagecolorallocate($image,?0,?0,?0); //用填充矩形填充畫布 imagefilledrectangle($image,?1,?1,?$width-2,?$height-2,?$white); //?產生隨機字符串 $chars?=?buidRandomString($type,?$length); //?echo?$chars."<br>"; //存儲到session $_SESSION[$sess_name]?=?$chars; //?字體數組 $fontfiles?=?array("hye3gjm.TTF","hyh4gjm.TTF","hyk1gjm.TTF","STKAITI.TTF","STLITI.TTF","STXIHEI.TTF","STXINGKA.TTF","STZHONGS.TTF"); //?隨機獲取數組中任意一個值 $fontfile?=?"../fonts/".$fontfiles[mt_rand(0,?count($fontfiles)-1)]; /*?將TTF?(TrueType?Fonts)?字型文字寫入圖片*/ for?($i=0;?$i?<?$length;?$i++)?{ //產生14?~?18的隨機數用于字體大小 $size?=?mt_rand(14,?18); //產生隨機數用于字符角度 $angle?=?mt_rand(-15,?15); //產生字符位置坐標 $x?=?5?+?$i?*?$size; $y?=?mt_rand(15,?20); //?產生隨機畫筆顏色,用于設置字體顏色 $color?=?imagecolorallocate($image,?mt_rand(50,?90),?mt_rand(80,?200),?mt_rand(90,?180)); $text?=?substr($chars,?$i,?1); imagettftext($image,?$size,?$angle,?$x,?$y,?$color,?$fontfile,?$text); } //?繪制點、線等干擾元素 if?($pixel)?{ for?($i=0;?$i?<?$pixel;?$i++)?{ imagesetpixel($image,?mt_rand(0,?$width-1),?mt_rand(0,?$height-1),?$black); } } if?($line)?{ for?($i=0;?$i?<?$line;?$i++)?{ $color?=?imagecolorallocate($image,?mt_rand(50,?90),?mt_rand(80,?200),?mt_rand(90,?180)); imageline($image,?mt_rand(0,?$width-1),?mt_rand(0,?$height-1),?mt_rand(0,?$width-1),?mt_rand(0,?$height-1),?$color); } } //?輸出圖片格式 header("content-type:image/gif"); //?生成圖片 imagegif($image); //?釋放資源 imagedestroy($image); } ????????verifyImage(2,?4,?10,?3);
憂郁的蛋撻
熊貓的家園 提問者
熊貓的家園 提問者 回復 憂郁的蛋撻
憂郁的蛋撻 回復 熊貓的家園 提問者
98開水1 回復 憂郁的蛋撻
憂郁的蛋撻 回復 98開水1
我的代碼給你試試。? ??
php 和mysql 升級了版本之后 API大部分都變了,當然不能直接運行。
舉報
手把手教你用PHP搭建電子商務平臺,由淺入深教你搭建電商系統
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-03-07
include.php中和image.func.pgp中的session開啟一個就可以了,都開啟就出不來驗證碼
2016-11-15
終于知道貼代碼方法了。以下代碼運行在 php?7.0.8 上通過。
include.php
string.func.php:
image.func.php :
2016-11-15
我的代碼給你試試。? ??
2016-11-15
php 和mysql 升級了版本之后 API大部分都變了,當然不能直接運行。