php判斷當前作業系統類型_php技巧

來源:互聯網
上載者:User

如何使用php擷取當前作業系統類型呢?嚴格來說這裡分兩種情況,一種情況是擷取伺服器端的作業系統類型,一種是擷取用戶端的作業系統類型。下面作者將對如何使用php擷取這兩種情況下的作業系統類型和大家做一些分享。
(1)php擷取伺服器端的作業系統類型
這個時候可以使用php系統內建的常量 PHP_OS 或者系統函數php_uname('s')關於這兩者返回的值可能的情況基本有如下幾種情況:

  • CYGWIN_NT-5.1
  • Darwin
  • FreeBSD
  • HP-UX
  • IRIX64
  • Linux
  • NetBSD
  • OpenBSD
  • SunOS
  • Unix
  • WIN32
  • WINNT
  • Windows
  • CYGWIN_NT-5.1
  • IRIX64
  • SunOS
  • HP-UX
  • OpenBSD

不過根據具體情況讀者還是自行列印出來結果看看最好,也許獲得的結果不在上述之列。
(2)php擷取用戶端的作業系統類型,這裡分享一個函數,比網上流傳的那些判斷更加精準,而且沒有bug,函數代碼如下:

function getOS(){  $os='';  $Agent=$_SERVER['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 6.0',$Agent)){   $os='Windows Vista';  }elseif(eregi('win',$Agent)&&eregi('nt 6.1',$Agent)){   $os='Windows 7';  }elseif(eregi('win',$Agent)&&eregi('nt 5.1',$Agent)){   $os='Windows XP';  }elseif(eregi('win',$Agent)&&eregi('nt',$Agent)){   $os='Windows NT';  }elseif(eregi('win',$Agent)&&ereg('32',$Agent)){   $os='Windows 32';  }elseif(eregi('linux',$Agent)){   $os='Linux';  }elseif(eregi('unix',$Agent)){   $os='Unix';  }else if(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';  }elseif($os==''){   $os='Unknown';  }  return $os; } 

根據伺服器端還是用戶端來決定採取的辦法,希望通過這篇文章大家知道如何使用php擷取當前作業系統類型。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.