2

Is there a possible way to redirect a user to a specific page after an amount of time? For example, 3 seconds.

I have already used the built-in function redirect from Slim, but it takes no arguments related to time. How can I use it to redirect after 3 seconds?

$app->redirect("/new_project/contact");
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
  • I'm afraid that is not possible to do that with Slim built-in function. You have to use either a `refresh` header or a javascript function to do so. Check the answers for this question: http://stackoverflow.com/questions/6119451/page-redirect-after-certain-time-php – Gustavo Straube Oct 03 '15 at 19:21
  • @GustavoStraube thanks a lot for you response! I did see that post you mention! Perhaps i could use the refresh header because i want to stick to php and not in javascript! – Konstantinos Pedarakis Oct 03 '15 at 19:25

1 Answers1

1

The way you are using redirect i presume, you are doing a web page and not an api, so you will probaly have the possitbility of going the Javascript route. Which for what i know, is the only way to solve your problem.

If you echo this into your html, it will redirect after 10 seconds. Be aware that the timeout argument is in milliseconds.

<script>

function redirect(page) {
    setTimeout(function () {
        window.location.href = page;
    }, 5000);
}

redirect("/new_project/contact");
</script>

This is probaly not what you are looking for, but is the only way to solve your initial problem statement.

mrhn
  • 17,961
  • 4
  • 27
  • 46
  • Thanks a lot for the response Martin! Yes! I ' ve used something similar with javascript, but again i want somehow to be able to do it with php! – Konstantinos Pedarakis Oct 08 '15 at 20:16
  • But php doesn't work in that way, Php serves webpages, it does not maintain a connection to the client and therefor doesn't care about how the webpage is received. Making it wait for the redirection is an anti pattern of the idea behind PHP. – mrhn Oct 08 '15 at 20:24
  • I think that most probably you are right! And this is what o have experienced with php so far! Somewhere in the internet though i have found something that was saying about this particular issue and because im using Slim framework here and im considered relative new, i wonder if it would be something, somehow to do it! I guess there is no way! Thanks Martin! I will mark as an answer! – Konstantinos Pedarakis Oct 08 '15 at 20:32
  • As another way of solving this problem in the future, is probaly going with an Javascript front end and a Backend which can probaly be Slim or any other backend framework. This way you get Javascript functionality with the power of a more conveniant backend language. – mrhn Oct 08 '15 at 20:43
  • Yes indeed! Thanks a lot Martin! – Konstantinos Pedarakis Oct 08 '15 at 21:10