PHP經驗集錦

huidaoli發表於2013-07-25
最近剛剛完成手中的專案,比較閒。來這兒轉轉,把積累的一些技巧分享給大家!
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\.]+)/","); $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; } //呼叫方法$browser=browseinfo() ;直接返回結果 3、獲取訪問者作業系統 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('NetBSD',$Agent)) { $os="NetBSD"; } elseif (eregi('BSD',$Agent)) { $os="BSD"; } elseif (ereg('OSF1',$Agent)) { $os="OSF1"; } elseif (ereg('IRIX',$Agent)) { $os="IRIX"; } elseif (eregi('FreeBSD',$Agent)) { $os="FreeBSD"; } if ($os=='') $os = "Unknown"; return $os; } //呼叫方法$os=os_infor() ; 4、檔案格式類 $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', 'avi' => '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-uuencode', 'uue' => 'text/x-uuencode', '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、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='../image/newinfor.gif'>":""; 方法二: $newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"<img src='../image/newinfor.gif'>":""; 7.資料庫封裝例子 <?php //------------------------------------------------------------------------------------------ // ※Database() 建構函式,資料庫初始引數 // ※Select() 查詢 // ※GetRows() 返回查詢的記錄總數 // ※Insert() 插入記錄 // ※Update() 更新 // ※Delete() 刪除 // ※Halt() 中斷並顯示錯誤資訊*/ //------------------------------------------------------------------------------------------ define("DATABASETYPE","1"); //定義資料庫型別:1為MySql;2為SQL Server;3為Oracle;4為Odbc define("SERVER","localhost"); //Host name or IP address of the database server define("DATABASE","dbName"); //要連線的資料庫名 define("USER","tableName"); //用於連線資料庫的使用者名稱 define("PASSWORD","paswd"); //用於連線資料庫的密碼 class Database{ var $dbLink; //連線控制程式碼 var $result; //查詢控制程式碼 var $insId; //Insert()成功返回AUTO_INCREMENT列的值 var $rows; //返回資料陣列 var $numRows; //返回資料數目 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() 返回為false無結果*/ function Select($table,$columns,$condition=1){ $sql="select $columns from $table where $condition "; //echo $sql."<br>"; $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:GetRows() 返回查詢的記錄總數*/ function GetRows($table,$condition=1){ $sql="select count(1) as count from $table where $condition"; //echo $sql."<br>"; $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("<b>Database Query Error:</b> %s<br>\n", $msg); printf("<b>MySql Error:</b> %s<br>\n",mysql_error()); }else echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=../include/error.htm'>";//自定一個出錯提示檔案 return false; } } switch($db->dbType){ case 1: @mysql_close(); break; case 2: break; } $db = new Database(); ?>