php 執行系統命令設定時間的方法

2021-06-06 19:58:16 字數 2774 閱讀 8994

**:

首先先要給大家介紹php執行linux系統命令的幾個基本函式。我曾經很長一段時間都分不清下面幾個函式的具體用法區別。

system函式

說明:執行外部程式並顯示輸出資料。

語法:string system(string command, int [return_var]);

返回值: 字串

詳細介紹:

本函式就像是 c 語中的函式 system(),用來執行指令,並輸出結果。若是 return_var 引數存在,則執行 command 之後的狀態會填入 return_var 中。同樣值得注意的是若需要處理使用者輸入的資料,而又要防止使用者耍花招破解系統,則可以使用 escapeshellcmd()。若 php 以模組式的執行,本函式會在每一行輸出後自動更新 web 伺服器的輸出緩衝暫存區。若需要完整的返回字串,且不想經過不必要的其它中間的輸出介面,可以使用 passthru()。

例項**:

< ?php

$last_line = system("ls", $retval);

echo "last line of the output: " . $last_line;

echo "return value: " . $retval;

?>

exec函式

說明:執行外部程式。

語法:string exec(string command, string [array], int [return_var]);

返回值: 字串

詳細介紹:

本函式執行輸入 command 的外部程式或外部指令。它的返回字串只是外部程式執行後返回的最後一行;若需要完整的返回字串,可以使用 passthru() 這個函式。

要是引數 array 存在,command 會將 array 加到引數中執行,若不欲 array 被處理,可以在執行 exec() 之前呼叫 unset()。若是 return_var 跟 array 二個引數都存在,則執行 command 之後的狀態會填入 return_var 中。

值得注意的是若需要處理使用者輸入的資料,而又要防止使用者耍花招破解系統,則可以使用 escapeshellcmd()。

例項**:

< ?php

echo exec("whoami");

?>

popen函式

說明:開啟檔案。

語法:int popen(string command, string mode);

返回值: 整數

詳細介紹:

本函式執行指令開檔,而該檔案是用管道方式處理的檔案。用本函式開啟的檔案只能是單向的 (只能讀或只能寫),而且一定要用 pclose() 關閉。在檔案操作上可使用 fgets()、fgetss() 與 fputs()。若是開檔發生錯誤,返回 false 值。

例項**:

< ?

$fp = popen( "/bin/ls", "r" );

?>

php呼叫linux命令啟動程式(root許可權)

1.<?php

echo shell_exec("id -a");

$last_line = system("/usr/sbin/phpsystem /bin/date -s '2010-05-28 13:10'", $retval);

echo 'last line of the output: ' . $last_line;

echo 'return value: ' . $retval;

?>

2.在/usr/local/apache2/htdocs/asterisk/目錄下建立檔案phpsystem.c

#include

#include

#include

#include

int main(int argc, char *argv)

//printf("/n/nafter setreuid uid :%u/n",getuid());

//printf("afer sertreuid euid :%u/n",geteuid());

int i;

char cmd[1024]=;

for(i=1;iif(strchr(argv[i],' '))

else

strcat(cmd,argv[i]);

strcat(cmd," ");

}printf( "\ncmd is: %s\n",cmd);

id = system(cmd);

//printf( "/nid=%d/n",id );

//id=-1:出現錯誤  

//id=0:呼叫成功但是沒有出現子程序  

//id>0:成功退出的子程序的id

return id;

}///

3.  儲存並編譯phpsystem.c

#gcc  -o phpsystem  -wall phpsystem.c

4.  賦許可權

#chmod u+s ./phpsystem  

5. 在瀏覽器中預覽結果

uid=48(apache) gid=48(apache) groups=48(apache) fri may 28 13:10:00 cst 2010 cmd is: /bin/date -s '2010-05-28 13:10' last line of the output: cmd is: /bin/date -s '2010-05-28 13:10'

return value: 0

PHP執行linux系統命令

本文是第一篇,講述如何在php中執行系統命令從而實現一些特殊的目的,比如監控伺服器負載,重啟mysql 更新svn 重啟apache等。首先先要給大家介紹php執行linux系統命令的幾個基本函式。我曾經很長一段時間都分不清下面幾個函式的具體用法區別。system函式 說明 執行外部程式並顯示輸出資...

php 執行系統命令函式

目錄 命令注入 system 函式 passthru 函式 exec shell exec 函式 反引號 popen 函式 pcntl exec 函式 命令注入 command injection 對一些函式的引數沒有做過濾或過濾不嚴導致的,可以執行系統或者應用指令 cmd命令或者bash命令 的一...

PHP執行系統命令函式

命令注入 command injection 對一些函式的引數沒有做過濾或過濾不嚴導致的,可以執行系統或者應用指令 cmd命令或者bash命令 的一種注入攻擊手段。常見的執行系統命令的函式有 system passthru exec shell exec popen proc open pcntl ...