Reputation: 1927
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, I need to exclude the page where the user attempts to login from requiring a valid session. I would like to be able to set the auto_prepend_file value on an per directory basis.
Environment: PHP 5.2.6m Apache 2, running php as a cgi not as mod_php, on Windows (if that matters) and on a machine that I have complete control over (not a hosted environment)
Using a htaccess file is out b/c I am not using mod_php. I have not been able to alter the in php.ini to set the auto_prepend_file, the server throws an internal error. And ini_set() does not work b/c it has already loaded the session checking file before I can change the value of auto_prepend_file.
I do not see a way to set auto_prepend_file on a per directory basis if you are not using mod_php/htaccess. Am I missing something?
Upvotes: 10
Views: 29487
Reputation: 43411
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>
Upvotes: -2
Reputation: 18598
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
Upvotes: 9
Reputation: 48304
Use .user.ini files.
http://php.net/manual/en/configuration.file.per-user.php
Upvotes: 3