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

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -