I'm facing a problem using Headers in PHP. header('location: http:google.com');
is not working. This is my code:
if(!headers_sent()){
header("location: http://google.com");
exit;
}
HTML Data...
Script is exiting but header()
is not working.
output_buffer
param and it's set to 4096. Later attempts and debugging steps concluded:
header("location: http://google.com";, true, 302);
made no difference header("Refresh: ...")
did succeed. HTTP response debugging (after being asked a couple of times):
StatusCode : 200 StatusDescription : OK Content : <br /> <b>Notice</b>: Undefined index: HTTP_ACCEPT_LANGUAGE in ... RawContent : HTTP/1.1 200 OK Transfer-Encoding: chunked Connection: keep-alive Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Content-Type: text/html; charset=UTF-8 Date: Fri, 18 May 201... Forms : {} Headers : {[Transfer-Encoding, chunked], [Connection, keep-alive],
Unanswered still:
header("Status: 303 etc");
vs. header('HTTP/1.1 301 Moved Permanently');
from the duplicate
State of cgi.rfc2616_headers
PHP SAPI
Running the sample code outside the phar.
Right, this is not so much as a fix to the problem but a work around to get redirects working client side while you're on your development server. I wrote up a middle ware function to generate the redirect headers for you, simply add it to your code then call Redirect("URL");
.
<?php
function Redirect($location, $delay=0) {
// Check to see if the header are already sent.
if(!headers_sent()) {
// Check to see if we want Refresh because Location isn't working.
if(defined('REDIRECT_ALTERNATIVE')) {
// We want Refresh, let's go!
header("Refresh: $delay; URL=\"$location\"");
exit;
}
// We want to do this server side, do it!
header("Location: $location");
exit;
}
}
Now that you have this function all that you need to do is in a file, preferably a constants or config file, use the code define('REDIRECT_ALTERNATIVE', true);
then that will enable client side redirecting, when you are done simply comment or remove that definition and it will switch back to server side.
Again this is not a solution as much as it is a work around, but after hours of testing with the OP this is what we managed to come up with.
Edit: For anyone who is wondering what the actual problem is, the OP's development server, for whatever reason, is always setting the http code to 200, even if we changed it manually, since I had no way to access his pc and debug his setup I came up with a workaround that avoids the need for his server to manage the redirects.
尝试这个。
header("Refresh: $seconds; URL=\"$location\"");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.