views:

73

answers:

2
header('Location: '. $url, TRUE, $http_response_code);

Sometimes the above takes several seconds, is it possible to speed it up?

+1  A: 

If you are having more code below this line which is not used now, then exit the script after redirection. This happens when you just have to redirect a old page.

 header('Location: '. $url, TRUE, $http_response_code);
 exit(0);
shamittomar
There is already `exit()` but still the problem exists.
wamp
Then the problem is not in this code but your connection to the server.
shamittomar
+1  A: 

What do you mean "several seconds"? Does it take several seconds for the server to send the header to the client? Or does it take several seconds for the client to load the new page?

In the first case, there's very little you can do to speed up. It's just a simple function call that outputs a few hundreds of bytes of data, at most. If there's high network latency or bad packet loss between the server sending the header and the client, then it could take several seconds for the data to be received and acted on. This you could fix, maybe, if you had multiple hosts and/or control over how your packets are routed.

But once the header's received by the client, it's entirely out of your hands. It may very well take a few seconds for the client to start a connection to the new location and send the request. You can't tell the client to hurry up and go faster.

Marc B