出現ThInkPHP驗證碼不顯示的情況
官方提示如下:
如果無法顯示驗證碼,請檢查:
① PHP是否已經安裝GD庫支持;
② 輸出之前是否有任何的輸出(尤其是UTF8的BOM頭信息輸出);(打開驗證碼文件為亂碼,也有可能是BOM編碼,可查看下方2尋找解決方法)
③ Image類庫是否正確導入;
④ 如果是中文驗證碼檢查是否有拷貝字體文件到類庫所在目錄;
⑤URL_MODEL模式,有些服務器不支持REWRITE 。
針對上面的情況,一一檢查
1.可以用phpinfo 查看GD庫是否開啟,phpinfo打印出來的頁面能搜到如下信息,說明安裝了GD庫
2.復制下面代碼,保存為php格式,上傳到服務器並運行,再刷新驗證碼。ps:下面代碼作用是把所有文件進行無BOM格式編碼
ps:把源代碼壓縮打包上傳到服務器,再用服務器解壓的情況,容易出現這種情況。ps:有些服務器打開文件之后,會以BOM格式保存文件。
<?php if (isset($_GET['dir'])){ //設置文件目錄 $basedir=$_GET['dir']; }else{ $basedir = '.'; } $auto = 1; checkdir($basedir); function checkdir($basedir){ if ($dh = opendir($basedir)) { while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..'){ if (!is_dir($basedir."/".$file)) { echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>"; }else{ $dirname = $basedir."/".$file; checkdir($dirname); } } } closedir($dh); } } function checkBOM ($filename) { global $auto; $contents = file_get_contents($filename); $charset[1] = substr($contents, 0, 1); $charset[2] = substr($contents, 1, 1); $charset[3] = substr($contents, 2, 1); if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { if ($auto == 1) { $rest = substr($contents, 3); rewrite ($filename, $rest); return ("<font color=red>BOM found, automatically removed.</font>"); } else { return ("<font color=red>BOM found.</font>"); } } else return ("BOM Not Found."); } function rewrite ($filename, $data) { $filenum = fopen($filename, "w"); flock($filenum, LOCK_EX); fwrite($filenum, $data); fclose($filenum); } ?>