去除bom头的php代码,如下:
<?php
/**
* 功能:去除bom头信息
* edit:www.
*/
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._<a href=http://blog.csdn.net/s394032675>csdn.net</a></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);
}
?>
附,bom头信息说明。
在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码。
PHP不能识别bom头,用记事本编辑utf-8编码后执行会出错。
去掉bom头,可以这样:
1、editplus去BOM头的方法
编辑器调整为UTF8编码格式后,保存的文件前面会多出一串隐藏的字符(也即是BOM),用于编辑器识别这个文件是否是以UTF8编码。 运行Editplus,点击工具,选择首选项,选中文件,UTF-8标识选择 总是删除签名,然后对PHP文件编辑和保存后的PHP文件就是不带BOM的了。
2、ultraedit去除bom头办法
打开文件后,另存为选项的编码格式里选择(utf-8 无bom头),确即可。
3、专门写的去除文件BOM头的程序,现在公布出来,可以放在项目根目录,然后运行。
您可能感兴趣的文章:
PHP实例:检测并清除文件开头的BOM信息Php批量去除bom头信息的实现代码
PHP 过滤页面中的BOM数据的简单实例
检测php文件是否有bom头的代码
批量清除php文件中bom的方法
检查并清除php文件中bom的函数
有关 UTF-8 BOM 导致样式错乱的解决方法
BOM与DOM的区别分析
有关UTF-8 编码中BOM的检测与删除
Here is a little example of how to benchmark or time something with php
这里有一段php代码,教大家如何去处理基准时间。
代码如下:
<?php
// a function to get microtime
function getmicrotime(){
list($usec, $sec) = explode()(" ",microtime());
return ((float)$usec + (float)$sec);
}
// start time
$time_start = getmicrotime();
// a little loop to time
for ($i=0; $i < 10000; $i++)
{
// print the loop number
echo $i.'<br />';
}
// the end time
$time_end = getmicrotime();
// subtract the start time from the end time to get the time taken
$time = $time_end - $time_start;
// echo a little message
echo '<br />Script ran for ' . round($time,2) .' seconds.';
?>
This will produce a list of numbers from 0 to 9999 in your browser and tell you how long it took too complete the iterations.
以上将产生一个从0到9999的数字列表,以及完成这些计算任务所消耗的时间。
总结:以上主要演示了microtime()函数的用法。
在php中,对用户密码进行加密后存储是必须的,这种需求可以考虑用md5函数来实现。
PHP中MD5函数的用法:
<?php
$pswd1=md5("cenusdesign");
echo $pswd1; //运行结果为:fc60ec37d1c08d5b0fb67a8cd934d5ba
$pswd2=md5("Cenusdesign");
echo $pswd2; //运行结果为:067577d9fc109c80538c81d6f02bd293
?>
经过md5加密后,原本“cenusdesign”转变成了一组32位的字符串,而且,即使是一个字母的大小写变化,这组字符串就会发生巨大的变化。
在做用户注册程序时,建议将密码首先经过MD5转换,然后将转换加密后的数据库。
在用户登陆时,也将密码先进行MD5转化,再和数据库中那组经过MD5加密的字符串进行比较。
这样对密码的安全性而言,确实高了不少。