Home  >  Article  >  Backend Development  >  The exec function in php is not what I thought_PHP Tutorial

The exec function in php is not what I thought_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:33:021198browse

I have been using php(as the current mainstream development language) for a long time, but I haven’t done much myself. I have never been exposed to functions such as exec before. I just read some mentioned in other people’s articles. Now I want to write a small function to modify the password of apache(the most popular WEB server platform on Unix platform). The implementation method has been clear for a long time, but I encountered a problem when using the exec function.

The function of the exec function is to execute system command line commands. Its syntax is:
string exec (string command [, array &output [, int &return_var]])

where command is the key The executed command string, output is the output content of the executed command, in array format, one line is a record in the array, return_var is the return value of the executed command.

In dos, execute the htpasswd command directly without adding any parameters, and you will get help similar to the following:
E:cvsworksvntools>htpasswd
Usage:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password
......

Therefore, for testing purposes, first use exec to execute this command to see the effect, php(as now mainstream development language)The code is as follows:
exec(' e:cvsworksvntoolshtpasswd.exe', $ar, $status);
for ($i=0; $i < count($ar) ; $i++) $ar[$i] = htmlspecialchars($ar[$i]);
$msg = implode('
' . " ", $ar);
echo $msg;

There is no error in the code execution, but there is no output. Why? Changing the command to dir or cd can execute it correctly, and replacing htpasswd.exe There is no problem if I switch to pk.exe. Is this related to htpasswd? After searching for an hour, I finally found the problem.

When using exec to execute htpasswd.exe without any parameters, The return value $status is 2, and after passing the parameter "-nsb 1 1", the return value is 0, and the output information is normal
1:{SHA}NWoZK3kTsExUV00Ywo1G5jlUKKs=

and execute htpasswd1.exe this When the file does not exist, the return value is 1 and there is no output information.

Therefore, my conclusion is that when the exec function executes the command normally and the return value is 0, there is output information. , and when the function execution command does not exist or returns an error code, there is no output information. When htpasswd.exe happens to be executed without any parameters, although it can print out help information, the return status is 2, so exec The function does not return any output information.

The system, passthru, shell_exec and other functions similar to exec should be similar.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508654.htmlTechArticleI have been using PHP (as the current mainstream development language) for a long time, but I have not done much personally. , exec and other functions have not been exposed before, I just read some mentioned in other people's articles. Now to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn