linux - crontab中執行的php檔案中遇到重新導向會如何?

來源:互聯網
上載者:User
有index.php檔案,其中包含代碼:

header("Location: /from_index.php"); # 重新導向(from_index.php的檔案位置在:/www/)

使用crontab -e加入定時任務:

*/5 9-23 * * * sleep 50 && php /www/index.php # php指令碼的絕對路徑

請問這樣的結構中,會執行from_index.php中的代碼嗎?

我瞭解到如果寫成:

*/5 9-23 * * * sleep 50 && curl -L -e '; auto' http://127.0.0.1/index.php # http訪問

是能夠執行到from_index.php中的代碼的。但是這樣的寫法是否沒有使用php命令效率高?

回複內容:

有index.php檔案,其中包含代碼:

header("Location: /from_index.php"); # 重新導向(from_index.php的檔案位置在:/www/)

使用crontab -e加入定時任務:

*/5 9-23 * * * sleep 50 && php /www/index.php # php指令碼的絕對路徑

請問這樣的結構中,會執行from_index.php中的代碼嗎?

我瞭解到如果寫成:

*/5 9-23 * * * sleep 50 && curl -L -e '; auto' http://127.0.0.1/index.php # http訪問

是能夠執行到from_index.php中的代碼的。但是這樣的寫法是否沒有使用php命令效率高?

php的執行
1.cli方式,直接用php指令碼執行

php  index.php

2.經過伺服器請求cgi方式

curl -L http://127.0.0.1/index.php

header函數跳轉,是伺服器響應給curl(相當於瀏覽器)做重新導向,然後重新請求伺服器
因此php /www/index.php,header函數是不會起作用的進行跳轉到index.php指令檔,因為沒有經過伺服器。

//index.phpheader("Location:demo.php");
//demo.phpecho "this is demo";

請求結果圖

伺服器接收到的請求圖

如果使用curl的話你需要開啟它的重新導向開關

curl 不可以使用 header

直接再在代碼裡面用 system 調用php命令執行那個php檔案 不可以這樣?

第一個問題,不會,
第二個,不會有直接執行cli的效能高

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.