- PHP: Detect if operating system is Windows.
- The code.
- PHP_OS
- PHP_SHLIB_SUFFIX
- PHP_OS_FAMILY
- Detect exact OS version from browser
- 11 Answers 11
- How can I detect if the user is on localhost in PHP?
- 10 Answers 10
- Find PHP version on windows command line
- 17 Answers 17
- Can PHP detect if its run from a cron job or from the command line?
- 21 Answers 21
PHP: Detect if operating system is Windows.
This is a PHP tutorial on how to check if the operating system is Windows. To do this, we simply check the value of the PHP_OS constant.
The code.
Let’s take a look at the code:
In the PHP example above:
- We start off by assuming that the operating system is not Windows. It is up to the rest of our script to prove this assumption wrong.
- We check the first three characters of the PHP_OS constant. If the first three characters are WIN, then we know that PHP is running on Windows.
- Finally, we print out a message for example purposes.
PHP_OS
PHP_OS is a predefined constant that contains a string. This string tells us which operating system PHP was built for. When I print out the PHP_OS constant on my Windows 7 machine, I get the following result:
Depending on what your operating system is, you might get results such as:
PHP_SHLIB_SUFFIX
You could also check the PHP_SHLIB_SUFFIX constant, which contains the build-platform’s shared library suffix. If PHP is running on a Windows operating system, then this constant will contain the string “dll”. An example using this approach:
Personally, I would stick to checking the PHP_OS constant unless you are running PHP 7.2. or above.
PHP_OS_FAMILY
If you are running PHP 7.2. or above, you can simply check the PHP_OS_FAMILY constant. This constant may contain the following values:
Hopefully, you found this article to be helpful!
Detect exact OS version from browser
I was wondering if there is a way I can detect the exact Operating System version from my browser using PHP/JS/ASP?
I know I can detect the type of OS (Windows XP,Windows Vista,OS X,etc) but I need to detect the exact version: Vista Business, Vista Ultimate, Windows XP Home, Windows XP Pro, etc.
11 Answers 11
Short answer: You can’t.
Long answer:
All you have is the information in the HTTP User-Agent header, which usually contains the OS name and version.
Usually, browsers running on Mac OS and Linux send enough information to identify the exact OS. For example, here’s my User-Agent header:
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.10 (intrepid) Firefox/3.0.7
You can see that I’m running Ubuntu 8.10 Intrepid Ibex.
And here’s what Firefox and Safari 4 Beta report on my MacBook Pro:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16
Windows browsers, on the other hand, usually only report the OS version and not the specific package (Pro, Business, etc.):
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x
After some googling I found this code and it seems to be working ok,(doesn’t detect Unix though)
// this will help you
In Classic ASP and ASP.NET use
This works best since it’s not interpreted code, this is running on the server.
You should really try to avoid doing something like that unless it’s absolutely necessary for the functionality of the web application.
Not all requests can come from a client running on windows.
Not all requests can come from a client that supports JavaScript.
The user-agent header may not be present in the request.
What is in the user-agent header may be missleading.
A well designed web application should provide complete content and functionality regardless of what’s in the user-agent header of the request or if the client supports javascript or any other clientside extension.
I don’t think you’ll be able to differentiate different versions of Vista, but you should be able to get the OS from the navigator object’s platform property. You’ll probably have to parse it, though, or differentiate based on it’s contents.
How can I detect if the user is on localhost in PHP?
In other words, how can I tell if the person using my web application is on the server it resides on? If I remember correctly, PHPMyAdmin does something like this for security reasons.
10 Answers 10
You can also use $_SERVER[‘REMOTE_ADDR’] for which IP address of the client requesting is given by the web server.
As a complement, as a function.
Newer OS users (Win 7, 8) may also find it necessary to include an IPV6-format remote address in their whitelist array:
$_SERVER[«REMOTE_ADDR»] should tell you the user’s IP. It’s spoofable, though.
Check this bounty question for a very detailed discussion.
I think what you remember with PHPMyAdmin is something different: Many MySQL Servers are configured so that they can only be accessed from localhost for security reasons.
I’m sorry but all these answers seem terrible to me. I would suggest rephrasing the question because in a sense all machines are «localhost».
The question should be; How do I run different code paths depending on which machine it is executed on.
In my opinion, the easiest way is to create a file called DEVMACHINE or whatever you want really and then simply check
Remember to exclude this file when uploading to the live hosting environment!
This solution is not depending on network configuration, it can not be spoofed and makes it easy to switch between running «live-code» and «dev-code».
It doesn’t seem you should use $_SERVER[‘HTTP_HOST’] , because this is the value in http header, easily faked.
You may use $_SERVER[«REMOTE_ADDR»] too, this is the more secure value, but it is also possible to fake. This remote_addr is the address where Apache returns result to.
If you want to have a whitelist / allowlist that supports static IPs and dynamic names.
This way you could set a list of names/IPs that will be able (for sure) to be detected. Dynamic names add more flexibility for accessing from different points.
You have two common options here, you could set a name in your local hosts file or you could just use one dynamic name provider that could be found anywhere.
This function CACHES results because gethostbyname is a very slow function.
For this pupose I’ve implemented this function:
For better reliability you could replace the $_SERVER[‘REMOTE_ADDR’] for the get_ip_address() that @Pekka mentioned in his post as «this bounty question»
Find PHP version on windows command line
I just tried to know version of my PHP from windows command typing, C:\> php -v But it is not working. It says php is not recognized as internal or external command .
17 Answers 17
In command prompt type below
Where C:\path\to\php is the folder where your php.exe file is located. After this run
than you will see something like
UPDATE:
If you find that when you restart your computer, this solution will not work anymore:
Set the Path like as following:
Step 1 — Click on the Windows icon
Step 2 — Click on the Settings icon
Step 3 — Click on System
Step 4 — Click on About
Step 5 — Click on System info
Step 6 — Click on Advanced system settings
Step 7 — Click on Environment variables.
Step 8 — Select Path row and then click Edit
Step 9 — Click New and then click Browse , then in the next panel which will open you need to select the folder you want in the Path . For the initial premise of this guide i will add the folder C:\Users\NewFolderInPath ♥
Note: Replace your PHP installed path(e.g: C:\Program Files\php ) with the above path ♥.
Step 10 — Click OK and click every OK button you will encounter to close every previous windows.
You just need to find out where is your PHP folder.
- If you are using XAMPP or WAMP then you will see a php folder.
- You just need to go into the php folder using your cmd using command
- And then just type in this command
- Then you will see something like
PHP 5.6.11 (cli) (built: Jul 9 2015 20:55:40) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
Then go to php folder directory, Suppose your php folder is in xampp folder on your c drive. Your command would then be:
After that, check your version:
This should give the following output:
PHP 7.2.0 (cli) (built: Nov 29 2017 00:17:00) ( ZTS MSVC15 (Visual C++ 2017) x86 ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
I have uploaded a youtube video myself about checking the version of PHP via command prompt in Bangla: https://www.youtube.com/watch?v=zVkhD_tv9ck
Go to c drive and run the command as below
xampp control panel->shell->type php-v you get the version of php of your xampp installed
DB(mariadb/mysql)version type localhost/phpmyadmin in url click enter click on sql type select version(); enter to get the mysql or mariaDb version
It is most likely that php is not in your specified path.
Try to issue the php command with the full path, for example:
C:\> «C:\Program Files\php\php.exe» -v
Please note, that this is just an example, your php installation might be in a different directory.
For Beginners to anything php, it is usually stored in the C:/ path folder of your PC (My Computer).
==On Windows==
1.Click Start Menu button
2.Type cmd and press enter to select the first program/application that responds to your search result.
A black window terminal will appear, this is known as a Command Line Interpreter
3.In the Terminal Window (Application) Type cd c: and press enter
4.Now type php -v and press enter
and viola there you’ll have the current php version that is installed in your machine
Can PHP detect if its run from a cron job or from the command line?
I’m looking for way to PHP to detect if a script was run from a manual invocation on a shell (me logging in and running it), or if it was run from the crontab entry.
I have various maintenance type scripts written in php that i have set to run in my crontab. Occasionally, and I need to run them manually ahead of schedule or if something failed/broken, i need to run them a couple times.
The problem with this is that I also have some external notifications set into the tasks (posting to twitter, sending an email, etc) that I DONT want to happen everytime I run the script manually.
I’m using php5 (if it matters), its a fairly standard linux server environment.
21 Answers 21
Instead of detecting when the script is run from the crontab, it’s probably easier to detect when you’re running it manually.
There are a lot of environment variables (in the $_ENV array) that are set when you run a script from the command line. What these are will vary depending on your sever setup and how you log in. In my environment, the following environment variables are set when running a script manually that aren’t present when running from cron:
There are others too. So for example if you always use SSH to access the box, then the following line would detect if the script is running from cron:
You can setup an extra parameter, or add a line in your crontab, perhaps:
And then you can check your environment variables for «CRON». Also, try checking the $SHELL variable, I’m not sure if/what cron sets it to.
Here’s what I use to discover where the script is executed from. Look at the php_sapi_name function for more information: http://www.php.net/manual/en/function.php-sapi-name.php
EDIT: If php_sapi_name() does not include cli (could be cli or cli_server) then we check if $_SERVER[‘REMOTE_ADDR’] is empty. When called from the command line, this should be empty.
The right approach is to use the posix_isatty() function on e.g. the stdout file descriptor, like so:
I think that the most universal solution is to add an environment variable to the cron command, and look for it on the code. It will work on every system.
If the command executed by the cron is, for example:
Then you can check it on the code:
I don’t know about PHP specifically but you could walk up the process tree until you found either init or cron.
Assuming PHP can get it’s own process ID and run external commands, it should be a matter of executing ps -ef | grep pid where pid is your own process ID and extract the parent process ID (PPID) from it.
Then do the same to that PPID until you either reach cron as a parent or init as a parent.
For example, this is my process tree and you can see the ownership chain, 1 -> 6386 -> 6390 -> 6408.
The same processes run under cron would look like:
This «walking up the process tree» solution means you don’t have to worry about introducing an artificial parameter to indicate whether you’re running under cron or not — you may forget to do it in your interactive session and stuff things up.