禁止個別 VirtualHost 或目錄使用 PHP mail() 函式


PHP 的 disable_functions 選項可以停用某些特定的 PHP 函式, 主要是基於安全理由, 將一些對系統有威脅的函式停用, 但 disable_functions 必須透過 php.ini 設定, 因為如果可以在 httpd.conf 或 .htaccess 設定, 使用者便很容易將停用的函式開啟。

之前遇到一個情況, 要將個別 VirtualHost 的 mail() 函式停用, 但主機內其他 VirtualHost 仍要繼續開啟 mail()。雖然 disable_functions 不能在 Apache 設定, 但另一個選項 “sendmail_path” 可以, 一般上 PHP 會自動對應到系統的 sendmail 路徑, 當使用 mail() 函式時, 便透過 sendmail 發出電郵, 那麼只要在 VirtualHost 或 Directory 下設定 sendmail_path 便可以了, 例如:

然後重新啟動 Apache:

# systemctl restart httpd

當停用 mail() 函式的 VirtualHost 或目錄使用 mail() 函式時, 程式不會報錯, 只是電郵發不出去。

Leave a Reply