How to detect X-Accel-Redirect (Nginx) / X-Sendfile (Apache) support in PHP?

后端 未结 3 1928
粉色の甜心
粉色の甜心 2020-12-29 10:22

About Application

I am working on an e-commerce application in PHP. To keep URL\'s secure, product download links are kept behind PHP. There is a file, say downloa

3条回答
  •  时光取名叫无心
    2020-12-29 11:03

    To detect if the mod_xsendfile apache module installed, you can try this code:

    if function_exists('apache_get_modules') 
          && in_array('mod_xsendfile', apache_get_modules()) { 
      header("X-Sendfile"); 
    }
    

    But this code just check if the module installed only, that can cause errors if it's installed but configured wrongly

    another possible way to do this to setup server-wide variable through Apache's .htaccess:

    
      
        XSendFile On
        XSendFileAllowAbove On
        SetEnv MOD_X_SENDFILE_ENABLED 1
      
    
    

    and check it form php code:

    if ($_SERVER['MOD_X_SENDFILE_ENABLED']) {
      Header(...)
    }
    

    The common idea is the same for nginx - just pass the value of status variable to backend via HTTP-header or CGI/FastCGI variable.

提交回复
热议问题