2016-02-29 110 views
2

我有一個運行外部進程的PHP腳本。Lighttpd緩存shell_exec()輸出?

外部進程輸出一個JSON字符串,其中包含系統中的數據,該數據會隨時更改。

當我運行外部進程時,我可以清楚地看到JSON字符串是每次運行它時都不相同的 。

但是,當我從Web瀏覽器使用AJAX調用我的PHP腳本時,首次調用PHP腳本後JSON字符串不會更新。

JSON字符串被更新的唯一時間是當我在網站內部時刷新頁面或手動運行外部過程。

這究竟是爲什麼?

LIGHTTPD緩存輸出嗎?

編輯:

,這裏是我的lighttpd配置文件的內容:

server.modules = (
    "mod_access", 
     "mod_cgi", 
    "mod_alias", 
    "mod_compress", 
    "mod_redirect", 
#  "mod_rewrite", 
) 

server.document-root  = "/var/www" 
server.upload-dirs   = ("/var/cache/lighttpd/uploads") 
server.errorlog    = "/var/log/lighttpd/error.log" 
server.pid-file    = "/var/run/lighttpd.pid" 
server.username    = "www-data" 
server.groupname   = "www-data" 
server.port     = 80 


index-file.names   = ("index.php", "index.html", "index.lighttpd.html") 
url.access-deny    = ("~", ".inc") 
static-file.exclude-extensions = (".php", ".pl", ".fcgi") 

compress.cache-dir   = "/var/cache/lighttpd/compress/" 
compress.filetype   = ("application/javascript", "text/css", "text/html", "text/plain") 

# default listening port for IPv6 falls back to the IPv4 port 
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port 
include_shell "/usr/share/lighttpd/create-mime.assign.pl" 
include_shell "/usr/share/lighttpd/include-conf-enabled.pl" 

$HTTP["url"] =~ "/cgi-bin/" { 
     cgi.assign = ("" => "") 
} 

cgi.assign  = (
     ".cgi" => "" 
) 

編輯

According to the following Google Product thread一個人叫johnjbarton提到了以下幾點:

好吧,我與Chrome緩存專家交談。在我的情況的問題是非常 這個錯誤可能描述:錯誤30862 - 動態插入 子資源不重新驗證,即使在包含文檔是 重載https://bugs.webkit.org/show_bug.cgi?id=30862

的力,刷新外部頁面加載僅軍隊資源。它 不強制刷新XHR加載的資源。這是一個dojo應用程序中的所有資源的幾乎 。

當我看着鉻的控制檯下Network部分,它說 所有我的網站提出的要求是XHR類型。

這可以以某種方式相關嗎?

編輯

我的請求標頭是

Accept */*

Content-Type application/x-www-form-urlencoded

非常重要的編輯 我編輯我的PHP腳本,使其追加的結果處理它執行到一個日誌文件。

事實證明,過程的輸出總是相同的。

但是,當我手動運行該過程時,它會不斷按預期更改。

在我看來好像LIGHTTPD緩存它執行的進程的輸出。

編輯

這是我的PHP腳本的樣子:?

<?php 
session_start(); 
if(!isset($_SESSION['user'])){ 
    header("Location: /"); 
} else { 
    header('Content-type: application/json'); 
    $output = shell_exec('/home/debian/myProcess/myProcess'); 
    file_put_contents("/var/log/lighttpd/access.log", $output, FILE_APPEND | LOCK_EX); 
    echo $output; 
} 

>

+0

你能發佈lighttpd配置的相關部分嗎? – Will

+0

在瀏覽器中檢查進行AJAX調用時的網絡流量,很可能會將結果緩存在瀏覽器中。使用任何用於AJAX的JS框架來檢查如何避免緩存AJAX響應,如果不使用框架,則必須爲每個查詢網址添加時間戳。 – Eborbob

+0

@Eborbob哦,好的。那麼,如何添加時間戳以及在哪裏添加此時間戳?我是否將其添加到客戶端或服務器端?你對我在哪裏可以閱讀更多關於這方面的建議有什麼建議嗎?最後:爲什麼時間戳會改變瀏覽器緩存的行爲? – vaid

回答

1

好了,我找到了問題的原因。

我正在執行的進程(我稱之爲myProcess)運行的進程本身名爲iwlist

iwlist似乎需要以root身份運行。

因爲我的PHP腳本執行任何過程稱爲www-data的lighttpd的服務器用戶,進程myProcesswww-data這反過來試圖執行iwlistwww-data也被執行,這是它都失敗了。

我的解決方案是通過守護進程運行myProcess,守護進程將myProcess的輸出轉儲到我的PHP腳本可以讀取的文件中。

這個效果很好。 `