php获取当前操作系统类型

PHP中文网
Release: 2023-02-28 15:38:01
Original
3142 people have browsed it

如何使用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 (没有列在Wikipedia)

不过根据具体情况读者还是自行打印出来结果看看最好,也许获得的结果不在上述之列。

(2)php获取客户端的操作系统类型

函数代码如下:

$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;
}
Copy after login

以上就是php获取当前操作系统类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!