0

I am quite new to php, I did some research and tried someone else's solution but it did not work out for me. I want to redirect users to another page after a certain code has been executed. I realized that there are no error messages and the site does not change. So I removed almost everything from the code and put it into a small test.php file. The same issue persists.

<!DOCTYPE html>
<html>
    <body>
        <h1>Tes2</h1>

        <?php
        // Execute some code here
        sleep(10); // Give the user 10 seconds to read the output of my code
        // redirect the user with php or trigger a JS script to do that
        header("Window-target: _parent");
        header("Location: https://www.w3schools.com/");
        ?>

    </body>
</html>

Expectation: The page should execute the main php script (visualized by the comment) and trigger a timer. When the timer ends, it should redirect me to "www.w3schools.com". There should be no errors or other messages. The redirect should be done by the php code, if possible (JS would be a possible way to solve this, but I would still need to start the JS code after my php code has been executed).

Result: The page shows up and loads the of the html code. The site remains and does not change. There are no errors.

Environment: Running on Chromium Version 96.0.4664.45 (Offizieller Build) for Linux Mint (64-Bit) The Website is functional and did execute PHP code as expected, but not this one.

Is there a light weight and universal (for most popular browsers) solution which will redirect the users to another page?

  • Pass in a meta redirect, see https://stackoverflow.com/questions/5411538/redirect-from-an-html-page – NVRM Dec 12 '21 at 15:32
  • @NVRM Seems like I was not clear enough with "I want to redirect users to another page after a certain code has been executed". I have to execute a php script after the user completed a form. After that, I can redirect the page – Daniel Götter Dec 12 '21 at 15:34
  • Yeah that's it. Try it. – NVRM Dec 12 '21 at 15:36
  • The code you suggested would redirect after a set amount of time from the point where the page has been loaded. That does not work for me. Or do you expect a visitor to be done with the form and read the result after X seconds? – Daniel Götter Dec 12 '21 at 15:42
  • I will edit my post accordingly – Daniel Götter Dec 12 '21 at 15:43
  • 3
    You cannot send a Location header (or any response header) after you’ve already output some HTML. You should be receiving a “headers already sent” notice, but your error reporting level might be hiding it. Move the php code to above your doctype and your redirect should work. – rickdenhaan Dec 12 '21 at 15:45
  • So you mean after the user has been shown the results they only have a short time to read them, and then they will be redirected? If you want a real-time timer for that you'd need JavaScript. But if you want the server simply to check if their time limit has expired next time they make a request, then you can do that with PHP. It isn't fully clear what you want - despite the very long post you haven't actually specified the requirements in much detail. – ADyson Dec 12 '21 at 15:53
  • To make the little PHP approach in your little test file work you need to take the PHP code, move it to the start, and make it exit after the redirect (for completeness) so ` ` ...etc – ADyson Dec 12 '21 at 15:55
  • @ADyson I did specify that I need to be able to redirect using PHP after my actual code has been executed. That is the only relevant thing and I think that I've mentioned it. Sorry if that is unclear – Daniel Götter Dec 12 '21 at 15:56
  • Thanks for answering my question. Now the only thing that I'm unsure about: What would be the best way to trigger the redirect? As stated, the redirect should happen, after some other php code has been executed. I guess that the best way would be to take a different approach and perhaps execute another file which would then head to the new url? How can I redirect my user, after the main php code has been executed? – Daniel Götter Dec 12 '21 at 16:01
  • 1
    Normally people specify the behaviour they want rather than specifying how it must be implemented. Often, a request for a very specific implementation which they can't make work is a sign of an XY problem. You didn't really say _why_ it must be done in PHP rather than JavaScript, although you've clarified that above I think. But anyway I've shown you how to get the PHP version working now – ADyson Dec 12 '21 at 16:01
  • As for how to trigger it, it depends on context but basically, call it after the code you want to be executed, but before your script executes anything. Not sure what else I can say to that without more info – ADyson Dec 12 '21 at 16:03
  • I will edit my post once more, please give me a moment for that – Daniel Götter Dec 12 '21 at 16:07

2 Answers2

3

Headers must be set before any data is transmitted, so you can't just stick them in the middle of a file. Quoting the the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

So at the very least you'll need to rewrite your file to:

<?php

  header("Window-target: _parent");
  header("Location: https://www.w3schools.com/");

?>
<!doctype html>
...  

Also, never sleep() in an http(s) response: that response should finish as fast as it can, no matter what content it needs to generate. Sleep has no place in (really any) PHP code.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
1

A combination of PHP and JS seems to be the easiest solution. But that might be only my opinion. I tried to document the code as good as possible, so others can understand it:

 <?php
        function redirect() {   // Create some JS code which will pause for 3 seconds and execute the move function afterwards. This Function will redirect the user
            echo "<script>";
            echo "function move() {window.location.replace('http://www.w3schools.com');}";
            echo "setTimeout(move, 3000);";
            echo "</script>";
        }
    ?>
    
    <!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
            <h1>Test2</h1>
            <?php
            echo "<p>You will be redirected after the code has been executed!</p>";
            // Run actual code
            redirect();     // Redirect using JS code
            ?>
        </body>
    </html>