search
Homephp教程php手册php判断当前操作系统类型,php判断当前

php判断当前操作系统类型,php判断当前

如何使用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获取当前操作系统类型。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function