php header location vs php_redirect

家住魔仙堡 提交于 2019-12-03 20:54:43

问题


What's the difference between the function "HTTP_redirect" and "header location" in PHP ?

When must I use the function "HTTP_redirect" ?

When must I use the function "header location" ?

Look that: http://php.net/manual/fr/function.http-redirect.php --> Manual for HTTP_redirect http://php.net/manual/fr/function.header.php --> Manual for the function header


回答1:


http_redirect is basically a helper function, making it easier to use header location by allowing you to pass an array for GET data.




回答2:


1) Header in PHP

header() function sends a raw HTTP header to a client.

<?php
header("HTTP/1.0 404 Not Found");
?>

The above (taken from the PHP documentation) sends a 404 header back to the client.

2) HTTP Redirect

Redirect to the given url.

<?php
http_redirect("relpath", array("name" => "value"), true, HTTP_REDIRECT_PERM);
?>

The above (taken from the PHP documentation) : Output

HTTP/1.1 301 Moved Permanently
X-Powered-By: PHP/5.2.2
Content-Type: text/html
Location: http://www.example.com/curdir/relpath?name=value&PHPSESSID=abc

Redirecting to <a href="http://www.example.com/curdir/relpath?name=value&PHPSESSID=abc">http://www.example.com/curdir/relpath?name=value&PHPSESSID=abc</a>.



回答3:


Header forwards the user to a new page, so PHP reinitializes, it's like a HTML meta redirection, but faster.



来源:https://stackoverflow.com/questions/14353130/php-header-location-vs-php-redirect

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