Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:20:35+00:00 2026-05-30T14:20:35+00:00

I want to run a PHP script in the background using another PHP script.

  • 0

I want to run a PHP script in the background using another PHP script.

I had this in mind:

exec('/usr/bin/php background.php &');

My webhost has only disabled access to exec().

The only other way I can think of is sending a mail() to an email forwarder which is piped to a script, but that’s rather a wacky workaround than a solution.

Does anyone know any solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:20:37+00:00Added an answer on May 30, 2026 at 2:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a shell script in php, but this shell script takes
I want run a php script weekly using a cron job, however the script
I want to run this PHP script http://db2express/imacs/radek/3.1/rationalTest.php?mode=create from Sinatra. Sinatra runs on a
I want to run a php script from the command line that is always
I want to run a web application on php and mysql, using the CakePHP
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to run a php script on my site from a newly created
I would like to run a PHP script from another PHP script so that
I'm using a php script to randomly show images. I've duplicated this script three
I have PHP script, which is requested by user. In this script I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.