Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are differences between location and refresh in codeginiter redirect function?

I would like to know what are differences between location and referesh in Codeigniter redirect() function?

https://www.codeigniter.com/user_guide/helpers/url_helper.html

like image 774
Mohammad Saberi Avatar asked Apr 13 '13 07:04

Mohammad Saberi


2 Answers

It does not have to do only with Codeigniter. These are the 2 methods that you can use to reload (or redirect) a page.

With Location: header you are sending a 3xx status code (usually 301 or 302) to the client's browser which usually indicates that the content has temporarily moved. Using the appropriate code will give more information to the client about the reason you are doing the redirection. This will be useful especially for search engines.

Also the browser does not have to download all the page's content before doing the redirect but it does it immediately as it gets the status code from the server and it goes to the new page instead. This way you do not break the 'back' button of the browser.

With Refresh meta tag or HTTP header you send a request to the client's browser to refresh the page without indicating any information about the reason you are doing it or the original and new content. Browser has to first download all the page content and then after the time (in seconds) specified in the Refresh it will redirect to the other page (usually 0 seconds).

Also if a user hits the 'back' button of his browser it will not work as it should since it will take him to the previous page and it will use Refresh again and send him to the next from where he pressed the button.

Above statements are according to W3C article here

like image 181
Vassilis Barzokas Avatar answered Nov 09 '22 23:11

Vassilis Barzokas


Codeigniter redirect method:

/**
 * Header Redirect
 *
 * Header redirect in two flavors
 * For very fine grained control over headers, you could use the Output
 * Library's set_header() function.
 *
 * @access  public
 * @param   string  the URL
 * @param   string  the method: location or redirect
 * @return  string
 */
if ( ! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = site_url($uri);
        }

        switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=".$uri);
                break;
            default         : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }
}

PHP header

http://php.net/manual/en/function.header.php

like image 25
itsme Avatar answered Nov 10 '22 00:11

itsme