How to set Php's auto_prepend_file directive per directory?

前端 未结 3 898
刺人心
刺人心 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:

    <?php
    {
      $script = <<<GA
    <script type="text/javascript">
      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);
      })();
    </script>
    GA;
      $replace = array('</body>','</BODY>');
      $page = str_replace($replace, "{$script}\r</body>", $page);
    //   $replace2 = array('</title>','</TITLE>');
    //   $page = str_replace($replace2, " - My Awesome Site</title>", $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 :

    <Directory "/path/to/folder">
        AddType application/x-httpd-php .html .htm
        php_value auto_prepend_file /absolute/path/to/apache-prepend.php
    </Directory>
    
    0 讨论(0)
  • 2020-12-06 07:28

    Use .user.ini files.

    http://php.net/manual/en/configuration.file.per-user.php

    0 讨论(0)
  • 2020-12-06 07:32

    You mention you can't use .htaccess files but can you make modifications to httpd.conf and do something like this:

    <Directory "c:/wamp/www/dev/">
        Php_value auto_prepend_file c:/wamp/www/dev/prepend.php
    </Directory>
    

    EDIT - just realised this doesnt work when running as CGI. I think though that one thing that will work is if you create a copy of your php.ini file, place it in the directory, and put your prepend directive in there like:

    auto_prepend_file = c:/wamp/www/dev/prepend.php
    
    0 讨论(0)
提交回复
热议问题