How to set Php's auto_prepend_file directive per directory?

前端 未结 3 903
刺人心
刺人心 2020-12-06 06:57

Background: I\'ve got some code that checks to see if a user has a valid session before processing the php page that I would like to set as the auto_prepend_file. However,

3条回答
  •  我在风中等你
    2020-12-06 07:18

    There is a great tutorial called: Automatically Include Files with PHP & Apache that explain how to do that with the apache directive and PHP code to append at the end. First, define a file to catch the page before it's outputted and append whatever you want:

    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-xxxxxxxxx-y']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    
    GA;
      $replace = array('','');
      $page = str_replace($replace, "{$script}\r", $page);
    //   $replace2 = array('','');
    //   $page = str_replace($replace2, " - My Awesome Site", $page);
      return $page;
    }
    ob_start("appendToFile");
    ?>
    

    Then add the Apache directive to your virtual host. You need to prepend the PHP file in order to use the ob_start() method :

    
        AddType application/x-httpd-php .html .htm
        php_value auto_prepend_file /absolute/path/to/apache-prepend.php
    
    

提交回复
热议问题