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?

13
  • Pass in a meta redirect, see stackoverflow.com/questions/5411538/redirect-from-an-html-page Commented Dec 12, 2021 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 Commented Dec 12, 2021 at 15:34
  • Yeah that's it. Try it. Commented Dec 12, 2021 at 15:36
  • 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. Commented Dec 12, 2021 at 15:45
  • 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 Commented Dec 12, 2021 at 16:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, thanks for your help
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.