View Full Version : Using htaccess conditionally prepend PHP
Pretend that I have an imaginary ".learn" extension, which is set to execute PHP. I want to prepend learn.php everytime Apache serves a .learn file (and only for this file-type, I want .php values to behave normally).
Any ideas how I would go about this?
schleppel
12-24-2005, 02:43 PM
This should work:
# set .learn extension to be PHP
AddType application/x-httpd-php .learn
# match the .learn extension
<FilesMatch "\.learn$">
# set the prepend file setting
php_value auto_prepend_file "learn.php"
</FilesMatch>
Edit: I put prepend instead of append, whoops.
Edit2: it was supposed to be prepend, i was right before.
_Aerospace_Eng_
12-25-2005, 09:18 AM
Is this how you would add a file to all pages like an advertisement for a subdomain or is that completely different?
schleppel
12-25-2005, 01:48 PM
Yes, it could be used to add an advert to a page, i believe auto_prepend_file and auto_append_file are just like includes, but done automatically.
Is this how you would add a file to all pages like an advertisement for a subdomain or is that completely different?
You would not want to prepend an advertisment - 99% of the time - I would say 100% of the time, but there is always an exception to the rule :)
Say you prepend your advertisement - out pops your ad, now you have a problem setting cookies, or doing anything else that involves having the headers sent to the server.
If you needed to do something like that, with php, you would want to use append (and both of these can be set server wide in the php.ini file) - even if you want it on top, you would still want to append, and then use javascript to place it on top, and force the page down (which may or may not be a good idea, depends on the use).
I just got here cause I needed the exact syntax of it (i never memorize syntax - far more efficient to fill your head full of what can be done, than the how - the how can be found in two seconds) - in my case I needed to prepend something for a directory - just make everything in it no cache - that particular use is not going to cause problems because its only dealing with headers, and not any actualy output to the screen. Another solid use of prepend is if you find yourself including the same files all the time - just build a nice library, put it into a prepend, and you now have those functions and whatever always avaible with out having to put them in each file.
Brajeshwar
11-22-2006, 10:45 AM
Ok, how would we do this if we want to do it domain or sub-domain specific?
Well, I am coming to this from Media Temples new (gs) issue where one single Php.ini governs all domains including alternate domains. I want to separately do an auto_prepend_file for the separate domains.
Thanking in anticipation
daniel142005
09-13-2007, 02:48 AM
You would not want to prepend an advertisment - 99% of the time - I would say 100% of the time, but there is always an exception to the rule :)
Say you prepend your advertisement - out pops your ad, now you have a problem setting cookies, or doing anything else that involves having the headers sent to the server.
If you needed to do something like that, with php, you would want to use append (and both of these can be set server wide in the php.ini file) - even if you want it on top, you would still want to append, and then use javascript to place it on top, and force the page down (which may or may not be a good idea, depends on the use).
I just got here cause I needed the exact syntax of it (i never memorize syntax - far more efficient to fill your head full of what can be done, than the how - the how can be found in two seconds) - in my case I needed to prepend something for a directory - just make everything in it no cache - that particular use is not going to cause problems because its only dealing with headers, and not any actualy output to the screen. Another solid use of prepend is if you find yourself including the same files all the time - just build a nice library, put it into a prepend, and you now have those functions and whatever always avaible with out having to put them in each file.
I am using php's append and prepend to put advertisements on my free hosting. I got a cheap reseller account from a friend and in the htaccess file for the accounts i added php to the following types ".htsysa" and ".htsysp" a = append, p = prepend, since the file starts with .ht most ftp programs will ignore it. Make sure you add php to the .html, .htm, etc file types so they will get the ads as well.
Now here is the part that prevents the cookies and header information from causing errors. PHP has a function called "ob_start", which is basically content buffering, put this in .htsysp
<?
ob_start();
?>
And this in .htsysa
<?
if(!is_array($_PREFERENCES))
{
$_EANIXPREFERENCES['TextAdLocation'] = "both";
}
$_OUTPUT = ob_get_contents();
ob_end_clean();
// You can do w/e u want to $_OUTPUT here, str replace, disable javascript, ANYTHING, the html code for the page is in this variable.
die($_OUTPUT);
?>
of course, you would have to make sure they know that they are not allowed to try to disable these ads, and you may want to add some randomness to them so its harder to disable them, but it is possible to do. If you have access to put the append and prepend files outside their reach, do it. Same goes for .htaccess, if you can modify httpd.conf, put em in there.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.