> 백엔드 개발 > PHP 튜토리얼 > PHP高手经验总结

PHP高手经验总结

PHP中文网
풀어 주다: 2023-02-28 10:20:01
원래의
866명이 탐색했습니다.

最近刚刚完成手中的项目,比较闲。来这儿转转,把积累的一些技巧分享给大家!
1、关于PHP重定向
方法一:

header("Location: index.php");
로그인 후 복사

方法二:

echo "<script>window.location =\"$PHP_SELF\";</script>";
로그인 후 복사

方法三:

echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=index.php\">";
로그인 후 복사


2、获取访问者浏览器

function browse_infor() { 
$browser="";$browserver=""; 
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
$Agent = $GLOBALS["HTTP_USER_AGENT"]; 
for ($i=0; $i<=7; $i++) { 
if (strpos($Agent,$Browsers[$i])) { 
$browser = $Browsers[$i]; 
$browserver =""; 
} 
} 
if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) { 
$temp =explode("(", $Agent); $Part=$temp[0]; 
$temp =explode("/", $Part); $browserver=$temp[1]; 
$temp =explode(" ",$browserver); $browserver=$temp[0]; 
$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver); 
$browserver = " $browserver"; 
$browser = "Netscape Navigator"; 
} 
if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent)) { 
$temp =explode("(", $Agent); $Part=$temp[1]; 
$temp =explode(")", $Part); $browserver=$temp[1]; 
$temp =explode(" ",$browserver);$browserver=$temp[2]; 
$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver); 
$browserver = " $browserver"; 
$browser = "Opera"; 
} 
if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent)) { 
$temp = explode("(", $Agent); $Part=$temp[1]; 
$temp = explode(";",$Part); $Part=$temp[1]; 
$temp = explode(" ",$Part);$browserver=$temp[2]; 
$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver); 
$browserver = " $browserver"; 
$browser = "Internet Explorer"; 
} 
if ($browser!="") { 
$browseinfo = "$browser$browserver"; 
}else { 
$browseinfo = "Unknown"; 
} 
return $browseinfo; 
} 
//调用方法$browser=browseinfo() ;直接返回结果
로그인 후 복사

3、获取访问者操作系统

function osinfo() { 
$os=""; 
$Agent = $GLOBALS["HTTP_USER_AGENT"]; 
if (eregi(&#39;win&#39;,$Agent) && strpos($Agent, &#39;95&#39;)) { 
$os="Windows 95"; 
} 
elseif (eregi(&#39;win 9x&#39;,$Agent) && strpos($Agent, &#39;4.90&#39;)) { 
$os="Windows ME"; 
} 
elseif (eregi(&#39;win&#39;,$Agent) && ereg(&#39;98&#39;,$Agent)) { 
$os="Windows 98"; 
} 
elseif (eregi(&#39;win&#39;,$Agent) && eregi(&#39;nt 5\.0&#39;,$Agent)) { 
$os="Windows 2000"; 
} 
elseif (eregi(&#39;win&#39;,$Agent) && eregi(&#39;nt&#39;,$Agent)) { 
$os="Windows NT"; 
} 
elseif (eregi(&#39;win&#39;,$Agent) && eregi(&#39;nt 5\.1&#39;,$Agent)) { 
$os="Windows XP"; 
} 
elseif (eregi(&#39;win&#39;,$Agent) && ereg(&#39;32&#39;,$Agent)) { 
$os="Windows 32"; 
} 
elseif (eregi(&#39;linux&#39;,$Agent)) { 
$os="Linux\"; 
} 
elseif (eregi(&#39;unix&#39;,$Agent)) { 
$os="Unix"; 
} 
elseif (eregi(&#39;sun&#39;,$Agent) && eregi(&#39;os&#39;,$Agent)) { 
$os="SunOS"; 
} 
elseif (eregi(&#39;ibm&#39;,$Agent) && eregi(&#39;os&#39;,$Agent)) { 
$os="IBM OS/2"; 
} 
elseif (eregi(&#39;Mac&#39;,$Agent) && eregi(&#39;PC&#39;,$Agent)) { 
$os="Macintosh"; 
} 
elseif (eregi(&#39;PowerPC&#39;,$Agent)) { 
$os="PowerPC"; 
} 
elseif (eregi(&#39;AIX&#39;,$Agent)) { 
$os="AIX"; 
} 
elseif (eregi(&#39;HPUX&#39;,$Agent)) { 
$os="HPUX"; 
} 
elseif (eregi(&#39;NetBSD&#39;,$Agent)) { 
$os="NetBSD"; 
} 
elseif (eregi(&#39;BSD&#39;,$Agent)) { 
$os="BSD"; 
} 
elseif (ereg(&#39;OSF1&#39;,$Agent)) { 
$os="OSF1"; 
} 
elseif (ereg(&#39;IRIX&#39;,$Agent)) { 
$os="IRIX"; 
} 
elseif (eregi(&#39;FreeBSD&#39;,$Agent)) { 
$os="FreeBSD\"; 
} 
if ($os==&#39;&#39;) $os = "Unknown"; 
return $os; 
} 
//调用方法$os=os_infor() ;
로그인 후 복사

4、文件格式类

$mime_types = array( 
&#39;gif&#39; => &#39;image/gif&#39;, 
&#39;jpg&#39; => &#39;image/jpeg&#39;, 
&#39;jpeg&#39; => &#39;image/jpeg&#39;, 
&#39;jpe&#39; => &#39;image/jpeg&#39;, 
&#39;bmp&#39; => &#39;image/bmp&#39;, 
&#39;png&#39; => &#39;image/png&#39;, 
&#39;tif&#39; => &#39;image/tiff&#39;, 
&#39;tiff&#39; => &#39;image/tiff&#39;, 
&#39;pict&#39; => &#39;image/x-pict&#39;, 
&#39;pic&#39; => &#39;image/x-pict&#39;, 
&#39;pct&#39; => &#39;image/x-pict&#39;, 
&#39;tif&#39; => &#39;image/tiff&#39;, 
&#39;tiff&#39; => &#39;image/tiff&#39;, 
&#39;psd&#39; => &#39;image/x-photoshop&#39;, 
&#39;swf&#39; => &#39;application/x-shockwave-flash&#39;, 
&#39;js&#39; => &#39;application/x-javascript&#39;, 
&#39;pdf&#39; => &#39;application/pdf&#39;, 
&#39;ps&#39; => &#39;application/postscript&#39;, 
&#39;eps&#39; => &#39;application/postscript&#39;, 
&#39;ai&#39; => &#39;application/postscript&#39;, 
&#39;wmf&#39; => &#39;application/x-msmetafile&#39;, 
&#39;css&#39; => &#39;text/css&#39;, 
&#39;htm&#39; => &#39;text/html&#39;, 
&#39;html&#39; => &#39;text/html&#39;, 
&#39;txt&#39; => &#39;text/plain&#39;, 
&#39;xml&#39; => &#39;text/xml&#39;, 
&#39;wml&#39; => &#39;text/wml&#39;, 
&#39;wbmp&#39; => &#39;image/vnd.wap.wbmp&#39;, 
&#39;mid&#39; => &#39;audio/midi&#39;, 
&#39;wav&#39; => &#39;audio/wav&#39;, 
&#39;mp3&#39; => &#39;audio/mpeg&#39;, 
&#39;mp2&#39; => &#39;audio/mpeg&#39;, 
&#39;avi&#39; => &#39;video/x-msvideo&#39;, 
&#39;mpeg&#39; => &#39;video/mpeg&#39;, 
&#39;mpg&#39; => &#39;video/mpeg&#39;, 
&#39;qt&#39; => &#39;video/quicktime&#39;, 
&#39;mov&#39; => &#39;video/quicktime&#39;, 
&#39;lha&#39; => &#39;application/x-lha&#39;, 
&#39;lzh&#39; => &#39;application/x-lha&#39;, 
&#39;z&#39; => &#39;application/x-compress&#39;, 
&#39;gtar&#39; => &#39;application/x-gtar&#39;, 
&#39;gz&#39; => &#39;application/x-gzip&#39;, 
&#39;gzip&#39; => &#39;application/x-gzip&#39;, 
&#39;tgz&#39; => &#39;application/x-gzip&#39;, 
&#39;tar&#39; => &#39;application/x-tar&#39;, 
&#39;bz2&#39; => &#39;application/bzip2&#39;, 
&#39;zip&#39; => &#39;application/zip&#39;, 
&#39;arj&#39; => &#39;application/x-arj&#39;, 
&#39;rar&#39; => &#39;application/x-rar-compressed&#39;, 
&#39;hqx&#39; => &#39;application/mac-binhex40&#39;, 
&#39;sit&#39; => &#39;application/x-stuffit&#39;, 
&#39;bin&#39; => &#39;application/x-macbinary&#39;, 
&#39;uu&#39; => &#39;text/x-uuencode&#39;, 
&#39;uue&#39; => &#39;text/x-uuencode&#39;, 
&#39;latex&#39;=> &#39;application/x-latex&#39;, 
&#39;ltx&#39; => &#39;application/x-latex&#39;, 
&#39;tcl&#39; => &#39;application/x-tcl&#39;, 
&#39;pgp&#39; => &#39;application/pgp&#39;, 
&#39;asc&#39; => &#39;application/pgp&#39;, 
&#39;exe&#39; => &#39;application/x-msdownload&#39;, 
&#39;doc&#39; => &#39;application/msword&#39;, 
&#39;rtf&#39; => &#39;application/rtf&#39;, 
&#39;xls&#39; => &#39;application/vnd.ms-excel&#39;, 
&#39;ppt&#39; => &#39;application/vnd.ms-powerpoint&#39;, 
&#39;mdb&#39; => &#39;application/x-msaccess&#39;, 
&#39;wri&#39; => &#39;application/x-mswrite&#39;, 
);
로그인 후 복사

5、php生成excel文档

<? 
header("Content-type:application/vnd.ms-excel"); 
header("Content-Disposition:filename=test.xls"); 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
echo "test1\t"; 
echo "test2\t\n"; 
?> 
//改动相应文件头就可以输出.doc .xls等文件格式了
로그인 후 복사

6、时间比较问题
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。
方法一:

//$db->rows[$i][date]中为数据库中datetime字段值. 
$today=time(); 
$theDay=date("Y-m-d H:i:s",$today-24*3600); 
$newTag=$db->rows[$i][date]>=$theDay?"<img src=&#39;../image/newinfor.gif&#39;>":"";
로그인 후 복사

方法二:

$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"<img src=&#39;../image/newinfor.gif&#39;>":"";
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿