The exec function in php is not like what I think _ PHP Tutorial

Source: Internet
Author: User
The exec function in php is not what I think. Using php (as the mainstream development language) is not short, but there are not many things I have done myself. exec and other functions have never been used before. I just read some of the articles mentioned in others. Currently, php (as the mainstream development language) is not short, but there are not many things I have done myself, and exec and other functions have never been used before, I just read some of the articles mentioned in others. Now I want to write a small function to modify the password of apache (the most popular WEB server platform on Unix). The implementation method has long been clear, but I encountered a problem when using the exec function.

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

Command is the command string to be executed, output is the output content of the command to be executed, the array format, one line is a record in the array, and return_var is the return value of the command to be executed.

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

Therefore, for the purpose of testing, run the command exec to check the effect. The php (as the mainstream development language) 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 code execution, but there is no output. why? It is no problem to replace the command with diror CDNs with pk.exe, which is related to htpasswd? After an hour of exploration, I finally found the problem.

When execute htpasswd.exe without any authorization using execute, the returned value $ status is 2, and after the parameter "-nsb 1 1 1" is included, the returned value is 0, and the output information is normal.
1: {SHA} NWoZK3kTsExUV00Ywo1G5jlUKKs =

When htpasswd1.exe does not exist, the returned value is 1 and no output information is displayed.

Therefore, my conclusion is that when the exec function runs normally and the return value is 0, the output information is available. when the function execution command does not exist or the error code is returned, no output information. When htpasswd.exe is executed without any parameters, although the help information can be printed, the returned status is 2, so the exec function does not return any output information.

Functions such as system, passthru, and shell_exec similar to exec should also be similar.

Pipeline (as the mainstream development language) is not short, but there are not many things I have done in person. exec and other functions have never been used before. I just read some of the articles mentioned in others. Now you want...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.