You could achieve that by using curl and loading the script as a web resource so it can be executed.
If that script needs to be protected from public, you can check if the request came from the same server:
if( $_SERVER['SERVER_NAME'] != 'localhost'
|| $_SERVER['REMOTE_ADDR'] != '127.0.0.1'
|| stripos( $_SERVER['HTTP_USER_AGENT'], 'wget' )===false
)
{
// Access Denied!!
die();
}
Be sure to make an asynchronous request with curl, so if the script takes a lot of time to execute, doesn't hang the original user request (use curl_setopt( $handle, CURLOPT_HEADER, false );
So the plan is from the page where you know you need to execute the script, launch an Http request to the script (this is a very basic example of using curl library):
$handle = curl_init();
curl_setopt( $handle, CURLOPT_URL, 'http://localhost/your_script.php');
curl_setopt( $handle, CURLOPT_HEADER, false );
curl_exec( $handle );
curl_close( $handle );
Then in your script, use the above script to protect requests not comming from current server, and do the job.
Updated
As stated in this question: sending a non-blocking HTTP POST request
In your script job you could send this header to close the connection as script is running:
// Send the response to the client
header('Connection: Close');
// Do the background job: just don't output anything!
Updated II
Reviewing my own answer, I've checked that curl_setopt( $handle, CURLOPT_HEADER, false ); does not create an asynchronous request. I still have not found how to do it.
system()orshell_exec()?