PHP加载page1,睡5秒,加载第2页

I need to do something that in theory sounds very easy but i've been running into alot of issues to get it done correctly.

There's a layer of complexity added because i'm running a custom frauddetection script but that shouldn't have any impact on this.

Basically, I need my PHP code to do the following

-Load Page 1 (through Include() so the URL doesn't change) -Show that page for 5 seconds -Stop showing Page 1, and instead show Page 2 (using an Include() again so the URl stays the same)

Any ideas?

Closest i've gotten is this:

<?php
ob_start();
include ("1b.html");
ob_clean();
sleep(5);
include ("1c.html");

 ?>

But it just displays nothing for 5 seconds, as if the page is still loading, and then just shows 1c without ever showing 1b for 5 seconds.

</div>

This is impossible. This is not at all how PHP works. You can't send a blob of data to the user, and then redact it and send a new blob of data to the client. Output buffering cannot be used in this way.

But it just displays nothing for 5 seconds, as if the page is still loading,

That's because the page is still loading. You're in the middle of an HTTP request, and you told PHP to sleep for 5 seconds.


To make this work, you need to send both parts to the client, and in JavaScript, programatically hide the first part and show the second after 5 seconds

Or

Send the first part to the user, and (either through a meta refresh or JavaScript) reload the page after 5 seconds and use some signal such as a query string parameter to session state to serve up the second page of content to the user.

It works for me:

<?php
if(session_status() == PHP_SESSION_NONE) session_start();

if(!isset($_SESSION['loaded'])) {
    $_SESSION['loaded'] = 0;
}

if($_SESSION['loaded'] == 0) {
    include('page1.php');
}
elseif($_SESSION['loaded'] == 1) {
    include('page2.php');
}
elseif($_SESSION['loaded'] == 2) {
    include('page3.php');
}
else {
    $_SESSION['loaded'] = 0;
    die(header("Refresh:0"));
}

if(isset($_SESSION['loaded'])) {
    $_SESSION['loaded']++;
    die(header("Refresh:5"));
}

- edit: only once:

<?php
if(session_status() == PHP_SESSION_NONE) session_start();

if(!isset($_SESSION['loaded'])) {
    $_SESSION['loaded'] = 0;
}

if($_SESSION['loaded'] == 0) {
    include('page1.php');
    $_SESSION['loaded']++;
    die(header("Refresh:5"));
}
elseif($_SESSION['loaded'] == 1) {
    include('page2.php');
}

Hope I helped :)