Some experience in PHP

Source: Internet
Author: User
Tags ereg gtar
Some experience accumulation in PHP and some experience accumulation in PHP, author: snowai recently completed a project in hand, relatively idle. Come here and share some accumulated skills with you! 1. PHP redirection Method 1: header ("> <LINKhref =" http://www.php100.com//statics/style/headfloor_95
Author: snowai

Recently, projects in the hands are relatively idle. Come here and share some accumulated skills with you!
1. PHP redirection
Method 1: header ("Location: index. php ");
Method 2: echo "script window. location = \" $ PHP_SELF \ "; script";
Method 3: echo" ";

2. obtain the visitor's browser

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;
}
// Call method $ browser = browseinfo (); directly return the result



3. obtain the visitor's operating system

Function osinfo (){
$ OS = "";
$ Agent = $ GLOBALS ["HTTP_USER_AGENT"];
If (eregi ('win', $ Agent) & strpos ($ Agent, '95 ')){
$ OS = "Windows 95 ";
}
Elseif (eregi ('win 9x', $ Agent) & strpos ($ Agent, '4. 90 ')){
$ OS = "Windows ME ";
}
Elseif (eregi ('win', $ Agent) & ereg ('98 ', $ Agent )){
$ OS = "Windows 98 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5 \. 0', $ Agent )){
$ OS = "Windows 2000 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt ', $ Agent )){
$ OS = "Windows NT ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5 \. 1', $ Agent )){
$ OS = "Windows XP ";
}
Elseif (eregi ('win', $ Agent) & ereg ('32', $ Agent )){
$ OS = "Windows 32 ";
}
Elseif (eregi ('Linux ', $ Agent )){
$ OS = "Linux ";
}
Elseif (eregi ('unix ', $ Agent )){
$ OS = "Unix ";
}
Elseif (eregi ('sun', $ Agent) & eregi ('OS', $ Agent )){
$ OS = "SunOS ";
}
Elseif (eregi ('IBM ', $ Agent) & eregi (' OS ', $ Agent )){
$ OS = "ibm OS/2 ";
}
Elseif (eregi ('Mac', $ Agent) & eregi ('PC', $ Agent )){
$ OS = "Macintosh ";
}
Elseif (eregi ('powerpc', $ Agent )){
$ OS = "PowerPC ";
}
Elseif (eregi ('aix ', $ Agent )){
$ OS = "AIX ";
}
Elseif (eregi ('hpux ', $ Agent )){
$ OS = "HPUX ";
}
Elseif (eregi ('netsd', $ Agent )){
$ OS = "NetBSD ";
}
Elseif (eregi ('bsd', $ Agent )){
$ OS = "BSD ";
}
Elseif (ereg ('oss1', $ Agent )){
$ OS = "OSF1 ";
}
Elseif (ereg ('irix ', $ Agent )){
$ OS = "IRIX ";
}
Elseif (eregi ('freebsd ', $ Agent )){
$ OS = "FreeBSD ";
}
If ($ OS = '') $ OS =" Unknown ";
Return $ OS;
}
// Call method $ OS = OS _infor ();



4. file format

$ Mime_types = array (
'Gif' => 'image/GIF ',
'Jpg '=> 'image/jpeg ',
'Jpeg '=> 'image/jpeg ',
'Jpe' => 'image/jpeg ',
'Bmp '=> 'image/bmp ',
'PNG '=> 'image/png ',
'Tif '=> 'image/tiff ',
'Tiff '=> 'image/tiff ',
'Pict' => 'image/x-pict ',
'Pic '=> 'image/x-pict ',
'Pct '=> 'image/x-pict ',
'Tif '=> 'image/tiff ',
'Tiff '=> 'image/tiff ',
'Psd '=> 'image/x-photoshop ',

'Swf '=> 'application/x-shockwave-Flash ',
'Js' => 'application/x-javascript ',
'PDF '=> 'application/pdf ',
'PS '=> 'application/postscript ',
'Eps '=> 'application/postscript ',
'AI' => 'application/postscript ',
'WMF '=> 'application/x-msmetafile ',

'Css '=> 'text/css ',
'Htm '=> 'text/html ',
'Html' => 'text/html ',
'Txt '=> 'text/plain ',
'Xml' => 'text/XML ',
'Wml' => 'text/wml ',
'Wbmp '=> 'image/vnd. wap. wbmp ',

'Mid '=> 'audio/midi ',
'Wav '=> 'audio/wav ',
'Mp3' => 'audio/mpeg ',
'Mp2' => 'audio/mpeg ',

'Av' => 'video/x-msvideo ',
'MPEG '=> 'video/mpeg ',
'Mpg' => 'video/mpeg ',
'Qt '=> 'video/quicktime ',
'Mov' => 'video/quicktime ',

'Lha' => 'application/x-lha ',
'Lzh '=> 'application/x-lha ',
'Z' => 'application/x-compress ',
'Gtar '=> 'application/x-gtar ',
'Gz '=> 'application/x-gzip ',
'Gzip '=> 'application/x-gzip ',
'Tgz' => 'application/x-gzip ',
'Tar '=> 'application/x-tar ',
'Bz2' => 'application/bzip2 ',
'Zip' => 'application/zip ',
'Arj' => 'application/x-arj ',
'Rar '=> 'application/x-rar-compressed ',

'Hqx' => 'application/mac-binhex40 ',
'Sit '=> 'application/x-stuffit ',
'Bin' => 'application/x-macbinary ',

'Uu '=> 'text/x-uencode ',
'Uuue' => 'text/x-uencode ',

'Latex '=> 'application/x-latex ',
'Ltx '=> 'application/x-latex ',
'Tcl '=> 'application/x-tcl ',

'Pgp '=> 'application/pgp ',
'Asc' => 'application/pgp ',
'Exe '=> 'application/x-msdownload ',
'Doc' => 'application/msword ',
'Rtf '=> 'application/rtf ',
'XLS '=> 'application/vnd. ms-excel ',
'Ppt '=> 'application/vnd. ms-powerpoint ',
'Mdb '=> 'application/x-msaccess ',
'Wri' => 'application/x-mswrite ',
);


5. generate an excel file in php


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 ";
?>
// You can change the corresponding file header to export file formats such as .doc. xls.


6. time comparison problems
For example, a forum marks a post posted on the day with a new image.
Method 1:
// $ Db-> rows [$ I] [date] indicates the datetime field value in the database.
$ Today = time ();
$ TheDay = date ("Y-m-d H: I: s", $ today-24*3600 );
$ NewTag = $ db-> rows [$ I] [date]> = $ theDay? "":"";
Method 2:
$ NewTag = $ db-> rows [$ I] [date]> = date ("Y-m-d 00:00:00 ")? "":"";
7. encapsulation of database operations in PHP
My example ...... see the attachment in this article!


Related attachment: example of database operation encapsulation in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.