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

无人久伴 提交于 2020-01-01 05:16:17

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15985537/what-are-differences-between-location-and-refresh-in-codeginiter-redirect-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!