permissions - PHP: Check if script is being ran with sudo or if user is not using `php` -
How to find out how the script is running with sudo access or not using PHP?
Note: this question would probably be more appropriate on stack overflow, though it can be used on Unix & amp; Refers to PHP; Linux system (privilege upgrades, permissions, etc.)
You can use PHP's POSIX functions:
- To get effective user IDs.
- To get user information from the UID.
Here is a short example:
& lt; Php $ userinfo = posix_getpwuid (posix_geteuid)); Echo "This script is running" $ Userinfo ["name"] "'Privileges.'"; ? & Gt;
test ...
$ php myfile.php This script runs with the privileges of my user. $ Sudo php myfile.php This script runs with the privileges of the root
Anyway, because the root is always UID 0, you can simply posix_geteuid () == 0
.
You now want that the user is using CLI (command line) or going through a web server, but have a look and function.
Another note: I'm pretty sure that running PHP script in the form of root is not the best of ideas. You can think again what permission your script is actually allowed.
Comments
Post a Comment