php header location vs php_redirect

时光怂恿深爱的人放手 提交于 2019-11-30 21:18:13

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

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>.

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

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