PHP auto include

后端 未结 3 1313
情书的邮戳
情书的邮戳 2021-01-17 19:32

I want to auto include a PHP script onto every exection into the server, I was hopping to do it via the PHP ini, via a setting or being able to write an extention in php tha

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 19:55

    If running Apache and can access .htaccess you can do the following otherwise look at @Lock's answer

    prepend.php:
    this is the prepended file

    \n"; main.php: this is the main file

    \n"; append.php: this is the appended file

    \n";

    And prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted from the script:

    this is the prepended file

    this is the main file

    this is the appended file

    And prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted from the script:

    this is the prepended file

    this is the main file

    this is the appended file

    Prepending a script

    To prepend a file so it is parsed before the main script, add the following setting to the .htaccess file, php.ini (which of course would affect all websites), or the config:

    php_value auto_prepend_file prepend.php
    

    A path does not need to be included but if it isn't then it will use the include path to find it. This can result in an error like "Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0" if there is no copy of this file in the include path or directory the script is in. So it's best to have the full path to the prepended file. Appending a script

    This is almost the same as prepending a script and the same notes apply. The way to append a file is as follows:

    php_value auto_append_file append.php
    

    Overriding a setting so nothing is prepended or appended

    If you need to override an existing auto_append_file or auto_prepend_file setting you can do this by setting the value to "none" like so:

    php_value auto_prepend_file none
    php_value auto_append_file none
    

    This can be useful if you want to have .htaccess set the append/prepend file at the root level of a website but then want a particular subdirectory to not do the append or prepend. You would create a new .htaccess file in that subdirectory which sets them to none as above.

    Source: http://www.electrictoolbox.com/php-automatically-append-prepend/

提交回复
热议问题