Possible Values For: PHP_OS
PHP passes through the uname, except on Windows (WINNT
) and Netware (Netware
). See Wikipedia for a non-exhaustive list of values not mentioned in your question:
- CYGWIN_NT-5.1
- IRIX64
- SunOS
- HP-UX
- OpenBSD (not in Wikipedia)
What is the name of your system with PHP_OS constant
Try php_uname for retrieving operating system information
PHP script - detect whether running under linux or Windows?
Check the value of the PHP_OS
constantDocs.
It will give you various values on Windows like WIN32
, WINNT
or Windows
.
See as well: Possible Values For: PHP_OS and php_uname
Docs:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!';
}
How to get the OS on which PHP is running?
PHP has many predefined constants that are often useful.
Here, PHP_OS
is the one you are looking for.
For instance, on my current machine, this code :
var_dump(PHP_OS);
Gives :
string 'Linux' (length=5)
You have some examples and comparisons with what the php_uname
function can get you on the manual page of php_uname
; for instance (quoting) :
<?php
echo php_uname();
echo PHP_OS;
/* Some possible outputs:
Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
Linux
FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001
FreeBSD
Windows NT XN1 5.1 build 2600
WINNT
*/
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!';
}
That page also says :
For the name of just the operating
system, consider using thePHP_OS
constant, but keep in mind this
constant will contain the operating
system PHP was built on.
Low overhead methodology to determine case sensitivity of OS
With the restriction of not being able to write to the file system i.e.
write A.txt
to a known location then try to read a.txt
I do not believe there is a way. Because even with OS sniffing (hacky at best and likely to give incorrect results) that is only referring to the OS... different filesystems on the os can behave differently (ie linux is case sensitive but the cifs share I have mounted is not)
I would say if knowlege of the case sensitivity of an os's filesystem it should be declared in code or config.
Or better build assuming case sensitivity but don't do anything that would break in a case insensitive environment, because anything that works on a case sensitive system will work on a case insensitive system (assuming you don't rely on the behavior of A.php and a.php being two different things which case sensitive or not is just dumb.)
Does PHP have a function to detect the OS it's running on?
PHP has included the constant PHP_EOL for solving the problem you face, available since php 4.3.10 and PHP 5.0.2 - it contains a suitable end-of-line sequence for the server that PHP is running on.
If you want to use a different end-of-line sequence suitable for a particular client, then you'll have to code that yourself. One way to determine the client OS is to use get_browser, assuming your server has an up-to-date browscap.ini
PHP_OS - does it contain OS it was built on or runs on?
I had a look at the source code. The configure script does this:
PHP_OS=`uname | xargs`
AC_DEFINE_UNQUOTED(PHP_OS,"$PHP_OS",[uname output])
and then main.c
does this:
char *php_os;
php_os=PHP_OS;
REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS", php_os, strlen(php_os), CONST_PERSISTENT | CONST_CS);
which means that PHP_OS
in PHP is what was in php_os
in the C code, which is what was #defined by the header file constructed by the configure script, which is determined by running uname
at configuration time.
So, it's the system you were built on, not the system you were running on.
There's some special-cased code for Windows: main.c
sets php_os
to "WINNT"
if WIN32
is defined. But, again, this is at build time, not at run time, even if you manage to compile on Unix and run on Windows or vice versa.
Related Topics
How to Enable Shell_Exec and Exec on PHP
Use Curl with Sni (Server Name Indication)
Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance
Importing Multiple CSV Files to MySQL Tables
Best Way to Document Array Options in PHPdoc
Php, Failed Connecting to Database
PHP "Pretty Print" JSON_Encode
Comparing Bcrypt Hash Between PHP and Nodejs
Where Exactly Do I Put a Session_Start
How to Get a User's Instagram Feed
Creating a Secure Login Using Sessions and Cookies in PHP
How to Minify Js in PHP Easily...Or Something Else
How to Get All Keys of an Multi Level Associative Arrays in PHP
Google-Like Search Engine in PHP/Mysql
Youtube API V3: How to Find a List of Each 'Videocategoryid'