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

  • SEARCH
  • Home
  • 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 8180633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:15:11+00:00 2026-06-07T00:15:11+00:00

I have two files client.php and server.php. The client file send a HTTP request

  • 0

I have two files client.php and server.php.
The client file send a HTTP request to the server file. The server file can be very slow to process the request so I just want it to answer to the client “OK the request is correct, the result will be sent by email”.

But I don’t know to make the server close the HTTP request with suitable headers and continue its job. If I specify a timeout of 1 second, I wont’t be able to know if the request will be accepted by the server.

So is it possible in PHP ? Do you know how ?

client.php:

<?php

$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, 'http://localhost/server.php');
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($resource, CURLOPT_TIMEOUT, 30);
curl_exec($resource);

server.php

<?php

header('200 OK');
echo 'OK the request is correct, the result will be sent by email';

// How to write the method below?
send_result_to_client();

// Simulates a slow process
sleep(60);

Ok I found the solution.
In the function send_result_to_client should seem to :

function send_result_to_client()
{
    $myString = '...';
    $size = strlen($myString);
    header("Content-Length: $size"); 
    header('Connection: close');
    flush();
}
  • 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-06-07T00:15:12+00:00Added an answer on June 7, 2026 at 12:15 am

    I’m sorry, but sending Connection:close and flush() is not the solution – not portably across servers or browsers. It may seem to be a solution because it appears to work when you run a simple test. If you try it with

     send_result_to_client();
     sleep(60);  // simulate a job...
     mail(...);  // ...ENDING with a mail message
    

    and then do nothing, or worse, do what any user will do – close the window as soon as he sees the “You’ll be notified by email” – you’ll discover that on most platforms, the email never gets sent, and the job never gets done.

    curl may terminate if Connection:close and Content-Length bytes have been received, but from RFC 2616,

    HTTP/1.1 defines the "close" connection option for **the sender** to signal that the connection will be closed after completion of the response.
    

    But the server is not closing the connection, not really. That would require a die() or exit() before the job has been processed.

    So your script continues executing only until the browser actually closes the connection – at that point (usually) the processing aborts. Maybe on some platforms it doesn’t, but don’t count on it.

    You may try to ameliorate the situation with another hack:

      set_time_limit(VERY_LONG_TIME);
      ignore_user_abort();
    

    but it is, indeed, a hack.

    There are no truly portable solutions. Usually, as suggested by Manatok, the job is queued somewhere else, and the queue processed asynchronously by another thread. This is possibly the best solution.

    Other possibilities involve a different and maybe simpler way to do the same thing, using atd and at command (or equivalent under Windows) to queue the job. You can create the “job control” with shell_exec by piping a new PHP script to be executed by atd:

    shell_exec("echo 'php -q /path/to/script.php \"param1\" \"param2\" | at now");
    

    (or more efficiently, running “at now” with popen and writing the command to execute to its stdin).

    See for example:

    https://github.com/treffynnon/PHP-at-Job-Queue-Wrapper

    You can also try to spawn a concurrent job, through popen or shell_exec, but you need to detach it from the server process, or you’ll find the system hogged by copies of the job executable (or CMD.EXE if you do this under Windows).

    See:

    Asynchronous shell exec in PHP

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

Sidebar

Related Questions

I have a script in PHP which retrieves two very similar files and performs
I have to write soap wsdl server in php. Requirement: request:two parameters say startDate
I have two perforce clients (client-a, client-b) that each contain about 40 file specs.
I have two files: domainList and config.cnf. The domainList file simply has a list
I have two files 1 - index.php 2 - main.php index.php call to main.php
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I have two files and the content is as follows: alt text http://img144.imageshack.us/img144/4423/screencapture2b.png alt
I have a xml file on my server. I have the following two questions.
I have two separate files that one is server-side JS. The other one is
I have php script from where user can upload the video file. The file

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.