Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    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?

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    360
    Thanks
    0
    Thanked 17 Times in 17 Posts
    This should work:
    Code:
    # 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.
    Last edited by schleppel; 12-24-2005 at 04:44 PM.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    PERFECT. Thanks a bunch!

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Is this how you would add a file to all pages like an advertisement for a subdomain or is that completely different?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    360
    Thanks
    0
    Thanked 17 Times in 17 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_
    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.

  • #7
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Domain Specific

    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

  • #8
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Zach View Post
    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
    PHP Code:
    <?
    ob_start
    ();
    ?>
    And this in .htsysa
    PHP Code:
    <?
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •