Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PersonalHomePage's avatar

Best Practices - redirect a page after some time has passed

Objective: redirect a page after 3 seconds.

Context: user submits input, gets a return redirect('/')->with('message', 'Success!');

I've searched online and have seen all sorts of solutions to this problem. A lot of people seem to favor doing a redirect using Javascript. Others suggest just using sleep() method with laravel. Seems like either one of these would be fine. I want to know what you guys think is best practice for this situation. Have you ever had problems using either one of those before?

For example I notice some websites will say Redirecting in 5 seconds... click here if it fails. Sometimes it does fail and since I don't know how they are redirecting, I would like to avoid that situation with my web app.

0 likes
6 replies
martinbean's avatar

@alex_b Why do you need to redirect after 3 seconds? If you’re redirecting in a controller, then you don’t need to show the “Redirecting in 5 seconds” message. Those messages are when a page is doing a redirect using a meta tag, not server-side coding. You can’t “hurry” a server-side redirect by clicking a link or button.

PersonalHomePage's avatar

That redirect just shows a message, but lets say I want to do another redirect without the user doing anything at that point. So I want them to see the message that everything worked, but than I want to redirect them to their previous page before they submitted anything. What would you suggest is the best approach to handle it?

martinbean's avatar

@alex_b I don’t really follow to be honest. Can you give an actual, concrete example of what you’re trying to do, rather than talking in wishy-washy terms of redirects upon redirects.

PersonalHomePage's avatar

What's the best way to redirect a user without them doing anything on the page. As I mentioned before I've seen php sites that try to redirect, but something fails in the request cycle and the redirect doesn't work. I guess I'm answering my own question here, but it would be best to use javascript to perform the redirect since it would be executed client side and could trigger the request cycle automagically on the server side. How would you handle doing a redirect without anything being done by the user?

Snapey's avatar
Snapey
Best Answer
Level 122

A normal person redirects with a message. On the redirected page, listen for any messages and show them in a popup. The user submits form, is redirected and immediately sees the message. After 5 seconds the message disappears and they carry on with what they want to do next.

PersonalHomePage's avatar

Oh okay I get it now. Between both your answers you guys filled in a lot of gaps for me. Thanks for the explanations guys, sorry if I did not make the question so clear. Hopefully I'll be as knowledgeable as you guys some day and can contribute back to the community too!

To summarize, I want to avoid the meta tag redirects like @martinbean mentioned (at least for now) and I can do a 5 second message like @snapey mentioned.

Please or to participate in this conversation.