编程语言
首页 > 编程语言> > 为什么PHP反引号和SSH不会返回相同的值?

为什么PHP反引号和SSH不会返回相同的值?

作者:互联网

当我使用ssh命令行运行ps cax时,我得到以下内容:

user@dqeb ~ $ps cax
PID TTY      STAT   TIME COMMAND
3277 ?        Ss    12:51 httpd
6797 ?        S      1:45 httpd
7190 ?        Ss     0:00 gpopd.pl
7291 ?        S      0:02 httpd
7303 ?        S      0:05 httpd
7309 ?        S      0:03 httpd
7336 ?        S      0:02 httpd
7361 ?        S      0:03 httpd
7419 ?        S      0:02 httpd
7426 ?        S      0:02 httpd
7427 ?        R      0:03 httpd
7440 ?        S      0:02 httpd
7457 ?        S      0:01 httpd
7468 ?        S      0:01 httpd
7504 ?        S      0:02 httpd
7743 ?        S      0:00 wrapper
7744 ?        Sl     0:00 java
7812 ?        S      0:00 qmail-local
7843 ?        S      0:00 qmail-local
7848 pts/3    R+     0:00 ps
8769 ?        Sl     0:00 sshd
8775 pts/5    Ss+    0:00 bash
9159 pts/2    S      0:00 su
9160 pts/2    S+     0:00 bash
9241 pts/5    S      0:00 gimap.pl
30334 ?        S      0:00 imap
30335 ?        S      0:00 imap
30340 ?        S      0:00 imap
30582 ?        Sl     0:00 sshd
30589 pts/3    Ss     0:00 bash

但是,当我运行以下PHP代码时:

$newline = chr(10);
$out = `ps cax`;
$out = str_replace($newline, '<br>', $out);
echo $out;

我明白了

7519 ? R 0:00 ps
15886 ? S 0:00 httpd
15890 ? S 0:00 httpd
15891 ? S 0:00 httpd
15917 ? S 0:00 httpd
15920 ? S 0:00 httpd
15930 ? S 0:00 httpd
15932 ? S 0:00 httpd
15933 ? S 0:00 httpd
16124 ? S 0:00 httpd
16125 ? S 0:00 httpd
16126 ? S 0:00 httpd
16128 ? S 0:00 httpd
16129 ? S 0:00 httpd
16130 ? S 0:00 httpd
16131 ? S 0:00 httpd
16134 ? S 0:00 httpd
16137 ? S 0:00 httpd
16138 ? S 0:00 httpd
16448 ? S 0:00 httpd

..而且这种情况持续了很长时间.

当我在同一台服务器上运行相同的命令时,为什么看不到相同的进程?我希望它们是完全相同的.

解决方法:

当您通过Web浏览器运行PHP脚本时,它将作为www用户执行,这是一个不太特权的用户.您只能看到www拥有的那些流程.这就是为什么你只看到httpd是apache子进程的原因.当您通过shell运行相同的脚本时,它将作为相应的用户(root或您用于ssh的用户名)运行.该用户可能拥有比www用户更多的权限.因此,您可以看到系统中几乎所有进程都在运行.

如果要在通过浏览器执行脚本时获得相同的结果,则需要升级www user的权限,这是一种安全威胁.浏览您网站的任何人都将获得相同的权限,他们可以轻松破解您的服务器.所以我不推荐它.

标签:shell-exec,php,shell,command,backticks
来源: https://codeday.me/bug/20190830/1765027.html