![]() In this case, mypage.php is the address of the page to which you would like to redirect the visitors. To redirect the visitor to another page (particularly useful in a conditional loop), simply use the following code: I've also handled the issue of supporting the different HTTP redirection codes ( 301, 302, 303 and 307), as it was addressed in the comments of my previous answer. Header(sprintf('Status: %03u', $code), true, $code) Session_write_close() // Avoids having sessions lock other requests Session_regenerate_id(true) // Avoids session fixation attacks If (strlen(session_id()) > 0) // If using sessions I've already answered this question, but I'll do it again since in the meanwhile I've learnt that there are special cases if you're running in CLI (redirects cannot happen and thus shouldn't exit()) or if your webserver is running PHP as a (F)CGI (it needs a previously set Status header to properly redirect). Then you might use a HTML header workaround (not very professional!) like: They usually fail if invoked inmidst HTML output. ![]() $url, true, $statusCode) Īs mentioned header() redirects only work before anything is written out. This is more flexible: function redirect($url, $statusCode = 303) This function doesn't incorporate the 303 status code: function Redirect($url, $permanent = false) You may use the alternative method of http_redirect($url) which needs the PECL package pecl to be installed. HTTP Headers and the header() function in PHP This is not true for many other user agents like spiders and robots. Currently used browsers are all HTTP/1.1 user agents. Note: W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. You should consider either 301 (permanent redirect) or 303 (other). PHP's "Location"-header still uses the HTTP 302-redirect code, this is a "temporary" redirect and may not be the one you should use. See RFC 7231 which had replaced the old RFC 2616, where only absolute URLs were allowed. Since June 2014 both absolute and relative URLs can be used. ![]() Why you should use die() or exit(): The Daily WTF ![]() You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the declaration, for example). Summary of existing answers plus my own two cents: 1. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |