PHP experience Collection, read PHP experience Collection, recently completed projects 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_950_081205.css "type = te
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 \.] + )/",");
$ 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 \.] + )/",");
$ 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 \.] + )/",");
$ 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. database encapsulation example
// Configure //------------------------------------------------------------------------------------------
// ※Database () constructor, initial Database parameter
// ※Select () query
// ※Getrows () returns the total number of queried records
// ※Insert () inserts a record.
// ※Update () Update
// ※Delete () Delete
// ※Halt () is interrupted and an error message is displayed */
// Configure //------------------------------------------------------------------------------------------
Define ("DATABASETYPE", "1"); // defines the database type: 1 for MySql; 2 for SQL Server; 3 for Oracle; 4 for Odbc
Define ("SERVER", "localhost"); // Host name or IP address of the database server
Define ("DATABASE", "dbName"); // name of the DATABASE to be connected
Define ("USER", "tableName"); // USER name used to connect to the database
Define ("PASSWORD", "paswd"); // PASSWORD used to connect to the database
Class Database {
Var $ dbLink; // connection handle
Var $ result; // query handle
Var $ insId; // Insert () returns the value of the AUTO_INCREMENT column
Var $ rows; // an array of returned data
Var $ numRows; // number of returned data
Var $ dbHost, $ dbUser, $ userPassword, $ database;
Var $ dbType = DATABASETYPE;
Var $ msgFlag = "yes"; // yes: show the Mysql message; no: die by show "Halted ."
Function Database ($ dbHost = SERVER, $ dbUser = USER, $ userPassword = PASSWORD, $ database = DATABASE ){
Switch ($ this-> dbType ){
Case 1:
$ This-> dbLink = @ mysql_pconnect ($ dbHost, $ dbUser, $ userPassword); // or die ("Can't Connect to Remote Host! ");
@ Mysql_select_db ($ database, $ this-> dbLink); // or die ("Can't Connect to Remote Host! ");
Break;
Case 2:
Break;
}
Return true;
}
/* SQL: Select () returns false without result */
Function Select ($ table, $ columns, $ condition = 1 ){
$ SQL = "select $ columns from $ table where $ condition ";
// Echo $ SQL ."
";
$ This-> result = @ mysql_query ($ SQL, $ this-> dbLink );
Unset ($ this-> rows );
If ($ this-> result ){
$ I = 0;
If (! ($ This-> rows = array ("$ I" => @ mysql_fetch_array ($ this-> result ))))
Return false;
If ($ this-> numRows = @ mysql_num_rows ($ this-> result) = 0)
Return false;
While ()){
Array_push ($ this-> rows, $ tempRows );
}
} Else {
$ This-> Halt ($ SQL );
Return false;
}
Return true;
}
/* SQL: total number of records returned by GetRows */
Function GetRows ($ table, $ condition = 1 ){
$ SQL = "select count (1) as count from $ table where $ condition ";
// Echo $ SQL ."
";
$ This-> result = @ mysql_query ($ SQL, $ this-> dbLink );
If ($ this-> result ){
);
$ This-> numRows = $ temp [count];
} Else {
$ This-> Halt ($ SQL );
Return false;
}
Return $ this-> numRows;
}
/* SQL: Insert ()*/
Function Insert ($ table, $ columns, $ values ){
$ SQL = "insert into $ table ($ columns) values ($ values )";
// Echo $ SQL;
$ This-> result = @ mysql_query ($ SQL, $ this-> dbLink );
If ($ this-> result)
$ This-> insId = @ mysql_insert_id ($ this-> dbLink );
Else {
$ This-> Halt ($ SQL );
Return false;
}
Return true;
}
/* SQL: Update ()*/
Function Update ($ table, $ setings, $ condition ){
$ SQL = "update $ table set $ setings where $ condition ";
// Echo $ SQL;
$ This-> result = @ mysql_query ($ SQL, $ this-> dbLink );
If ($ this-> result)
$ This-> numRows = @ mysql_affected_rows ($ this-> result );
Else {
$ This-> Halt ($ SQL );
Return false;
}
Return true;
}
/* SQL: Delete */
Function Delete ($ table, $ condition ){
$ SQL = "delete from $ table where $ condition ";
$ This-> result = @ mysql_query ($ SQL, $ this-> dbLink );
If ($ this-> result)
$ This-> numRows = @ mysql_affected_rows ($ this-> result );
Else {
$ This-> Halt ($ SQL );
Return false;
}
Return true;
}
/* Halt (): error message */
Function Halt ($ msg ){
If ($ this-> msgFlag = "yes "){
Printf ("Database Query Error:% S
\ N ", $ msg );
Printf ("MySql Error:% S
\ N ", mysql_error ());
} Else
Echo" "; // Customize an error prompt file
Return false;
}
}
Switch ($ db-> dbType ){
Case 1:
@ Mysql_close ();
Break;
Case 2:
Break;
}
$ Db = new Database ();
?>