skip to Main Content

I need to run a PHP script every time someone visits a URL on my web page. how can I do this with apache2.4 and PHP-FPM? I also needed to preserve all the data (like the headers) from Apache. I am trying to use PHP and on every url even the errors.

2

Answers


  1. You can just include the file containing the code (file.php) you want to execute in top of your php code like this and it will execute everytime someone visits your original php file:

     <?php
     include 'file.php';
     // rest of your code goes below
     
    

    If you want to preserve the header information too. Capture that in some variable and array and pass that to your file.php.

    For passing the header information, you might need a function for that in your file.php file.

    You can send those information as parameters via calling your function. Better to use classes.

    Login or Signup to reply.
  2. What you can do is to have php.ini autoprepend a script to each of your pages.

    This should be a starting point for you:

    # Prepend file to top of page
    auto_prepend_file = '/yourpath/pre_header.php'
    

    Note that php.ini is server related so this will affect all sites you host unless you do something like what is described here to have a php.ini for each site.

    Regarding the content of the script and what to do with the information you are free to do whatever you want with that script. The pro is that you don’t have to modify any file and this will be executed for any php page your server is serving

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search