0

I have an app which needs to show an alert at a certain time and I need to run some PHP in the background.

echo "My content";

// Run below code asynchronously without interrupting the client's page from loading

sleep(60);
header("location: alert.php");

Instead of the client's browser waiting for all the PHP to finish, I want it to load and then to display the alert after 60 seconds. I also cannot use JavaScript for this for security reasons.

5
  • 1
    You need some clientside code to trigger the async request Commented May 8, 2020 at 6:26
  • 1
    Please explain the security issues you're concerned about. Perhaps on the PHP side you could return different data after whatever delay you need, but you'll still need to do something in Javascript to perform that late load. Commented May 8, 2020 at 19:48
  • 1
    The alert must show up. In reality, it's a redirect. Commented May 9, 2020 at 0:38
  • 1
    The content can only be displayed for a limited amount of time, and then it must redirect. I've though about using JS, but it could somehow be stopped, couldn't it? setTimeout(myRedirectFunc, 60000) Commented May 9, 2020 at 0:40
  • 1
    Are you sure the PHP always takes exactly 60 seconds? Why not show the alert after the PHP finishes, even if it takes a bit more or less than 60 seconds? Commented May 9, 2020 at 2:25

1 Answer 1

1

You're saying "alert" but do you mean a Javascript alert() or some other sort of alert like a block of HTML, or perhaps an HTML modal?

In any case, why not just have your PHP script do this:

ob_end_flush(); // Disable output buffering
echo "My content. Please wait...";
do_some_stuff_that_takes_a_while();
echo "<script>alert('Done!');</script>";
Sign up to request clarification or add additional context in comments.

3 Comments

Firstly, it's a html file, see the question, and secondly, it blocks. Your code doesn't echo the content. It just keeps on loading until do_some_stuff_that_takes_a_while() finishes.
What is an HTML file? The alert? The original question doesn't mention "HTML". In any case, are you saying that "My content..." doesn't show until do_some_stuff_that_takes_a_while() finishes? That may be because your system caches/buffers in some other way. ob_end_flush() disables PHP's caching, but caching may still be happening at the server or proxy level. If nothing else, try adding a \n to the "please wait..." message to see if that flushes the buffer.
It redirects to a html file: location: alert.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.