CodeIgniter Forums

Full Version: Redirect Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]AgentPhoenix[/eluser]
I'm working on a login script for my application and I want users to fill the form out and submit it. The form then kicks them over to a new page that checks the user details to see if they're allowed in. If they are, it kicks them over to a success page, displays a message, then 5 seconds later, spits them out at the control panel. Simple enough. Going from the login page to the check_login page is easy because it's a form tag, but I'm running in to an issue with the redirect function.

Since there's nothing in the function for handling delay before a redirect, I tried to use PHP's sleep function to delay the redirect, but instead of going from check_login to success and waiting, it's waiting on check_login for 5 seconds then blowing through success straight into the control panel. This happens whether I use location or refresh. Any ideas how I hold it up on success for a few seconds before going over to the control panel?

El Forum

[eluser]gtech[/eluser]
The reason it waits on the check_login page is because when it loads the success page, it will not be displayed until content has been returned from the server.. so all you are doing is sleeping before the server can return the HTML of the success page to the browser.

there are some solutions I can think of (I am assuming you want to stay away from javascript)

1>have a form on the success page that a user clicks ok
2>rather than use the CI redirect function use the HTML redirect in a view where you can set the delay time to 5 seconds.. this way the content gets returned from the server and the browser does the wait.

Code:
<html>
  <head>
    <meta HTTP-EQUIV="REFRESH" content="5; url=http://www.yourdomain.com/index/controller/method">
  </head>

  <body>
    <h1>Well Done You Have Logged In</h1>
  &lt;/body&gt;
&lt;/html&gt;

3>have another step after the success page that sleeps for 5 seconds and then redirects to the 'home' page.


I prefer solution 2.

El Forum

[eluser]AgentPhoenix[/eluser]
Solution 2 is how I'm doing in the current iteration of the system, but I'm using the Layout Library now, so I'm not sure how things are gonna freak out if I try putting a meta tag down in the body. I guess I could abandon the Layout Library for that page and just use a standard view though. Not idea, but it'll work. Thanks for the quick response, gtech! Smile

El Forum

[eluser]gtech[/eluser]
No probs.

if you look at the url_helper.php helper that has the redirect function



Code:
if (! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location')
    {
        switch($method)
        {
            case 'refresh'    : header("Refresh:0;url=".site_url($uri));
                break;
            default            : header("Location: ".site_url($uri));
                break;
        }
        exit;
    }
}

note the Refresh bit in setting the header has a 0 next to it..

so in your success controller code all you need to do is
Code:
...
  $uri = 'controller/method';
  header("Refresh:5;url=".site_url($uri));
..

header is a php function that will add to the existing headers so nothing should in theory break.

worth a try anyway, if that works why not ask for a feature request to be able to pass a wait time to the redirect function (if it hasn't been asked already)

El Forum

[eluser]AgentPhoenix[/eluser]
Very cool! At the very least, I can edit the redirect helper to accept a wait parameter. I might submit it as a feature request as well. Thanks!

El Forum

[eluser]gtech[/eluser]
your welcome!