Execute PHP Script Before Every PHP Script

Execute php script before every php script?

Put it in its own file and set the auto_prepend_file configuration in the php.ini / .htaccess file to point to it.

Update: Since you mentioned lighttpd in a comment, note that you can configure it like this in the global INI file with PHP 5.3:

[PATH=/vhost/domain.com]  
auto_prepend_file = /vhost/domain.com/foo.php

[HOST=domain.com]
auto_prepend_file = /vhost/domain.com/foo.php

Or you can create the file /vhost/domain.com/.user.ini and do the same:

auto_prepend_file = /vhost/domain.com/foo.php

Run PHP script on all pages

Having a single file act as a controller that loads all your other files (perhaps index.php) is a common way, but if that's not possible, I'd suggest prepend files as mentioned by Matt Browne. (Apache dependent.) I actually did an append to force a footer on a site that was all static pages. I had to do it in php.ini, not .htaccess.

# Append file to bottom of page
auto_append_file = '/home/mycustomer/public_html/sub_footer.php'

So, yours could be

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

Execute a PHP script from another PHP script

You can invoke a PHP script manually from the command line

hello.php
<?php
echo 'hello world!';
?>

Command line:
php hello.php

Output:
hello world!

See the documentation: http://php.net/manual/en/features.commandline.php


EDIT OP edited the question to add a critical detail: the script is to be executed by another script.

There are a couple of approaches. First and easiest, you could simply include the file. When you include a file, the code within is "executed" (actually, interpreted). Any code that is not within a function or class body will be processed immediately. Take a look at the documentation for include (docs) and/or require (docs) (note: include_once and require_once are related, but different in an important way. Check out the documents to understand the difference) Your code would look like this:

 include('hello.php');
/* output
hello world!
*/

Second and slightly more complex is to use shell_exec (docs). With shell_exec, you will call the php binary and pass the desired script as the argument. Your code would look like this:

$output = shell_exec('php hello.php');
echo "<pre>$output</pre>";
/* output
hello world!
*/

Finally, and most complex, you could use the CURL library to call the file as though it were requested via a browser. Check out the CURL library documentation here: http://us2.php.net/manual/en/ref.curl.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.myDomain.com/hello.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)

$output = curl_exec($ch);
curl_close($ch);
echo "<pre>$output</pre>";
/* output
hello world!
*/

Documentation for functions used

  • Command line: http://php.net/manual/en/features.commandline.php
  • include: http://us2.php.net/manual/en/function.include.php
  • require: http://us2.php.net/manual/en/function.require.php
  • shell_exec: http://us2.php.net/manual/en/function.shell-exec.php
  • curl_init: http://us2.php.net/manual/en/function.curl-init.php
  • curl_setopt: http://us2.php.net/manual/en/function.curl-setopt.php
  • curl_exec: http://us2.php.net/manual/en/function.curl-exec.php
  • curl_close: http://us2.php.net/manual/en/function.curl-close.php

How can I run PHP script in certain interval (e.g. once a day)?

If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.

Run PHP Script Only When Called From Certain Page

You can define a constant within your index.php, that would not exist had the included script been called directly. In your included script you check if this constant is set and stop execution if it isn't.

Your original script (index.php):

define('PROPERLY_STARTED', true);
include_once 'a.php';

Your a.php:

if (!defined('PROPERLY_STARTED')) return;

Because my comment was deleted for some reason: While this works, it's error prone as you need to add that code to every single file. The established way of dealing with this issue is to only expose the index.php in your web root and have the files that should remain inaccessible in a directory outside of your web root so they aren't even reachable via HTTP (see e.g. the accepted answer on the question this is marked as duplicate of)

How to execute a php script from another?

If you do not want to wait for them to finish, run them with either

exec('php script.php &> /dev/null &');
shell_exec('php script.php &> /dev/null &');
system('php script.php &> /dev/null &');
`php script.php &> /dev/null &`

Any of those should accomplish the job, depending on your PHPs configuration. Although they are different functions, their behaviour should be similar since all output is being redirected to /dev/null and the proccess is immediately detached.

I use the first solution in a production environment where a client launches a bash SMSs sending script which can take up to 10 minutes to finish, it has never failed.

More info in: http://php.net/exec · http://php.net/shell_exec · http://php.net/system



Related Topics



Leave a reply



Submit