ThinkPHP验证码乱码解决思路
本内容介绍了在ThinkPHP框架中解决验证码页面出现乱码问题的具体方案,供开发者参考。
在进行ThinkPHP项目开发过程中,偶尔会遭遇验证码无法正常显示而呈现乱码的情况。针对这一现象,可将下述代码保存为独立文件置于项目根目录下,执行访问后即可修复问题。相关PHP实现代码如下:
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")." "; }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 ("BOM found, automatically removed."); } else { return ("BOM found."); } } else return ("BOM Not Found."); } function rewrite ($filename, $data) { $filenum = fopen($filename, "w"); flock($filenum, LOCK_EX); fwrite($filenum, $data); fclose($filenum); } ?>
期望上述解决方案能为使用ThinkPHP框架进行PHP程序开发的读者提供实质性的帮助。