tags:

views:

244

answers:

4

I have a large PHP application. After I changed some settings I get a redirection loop (i.e. the browser is redirected to the same page over and over again).

The problem is that I don't know which command (which line in which PHP file) in this application causes the redirect. Is there a way to trace calls to the header() function? Or - even better - is there a way to trace redirects in PHP?

Thanks in advance,
Michel

A: 

If your application is redirecting to the same URL, you should check that before redirecting, for example:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($targetHost !== $_SERVER['HTTP_HOST'] &&
    $targetPath !== $_SERVER['REQUEST_URI_PATH'] &&
    $targetQuery !== $_SERVER['QUERY_STRING']) {
    // redirect
}
Gumbo
Thanks. That's what I would like to do, but I don't know where to put this code, because I don't know which instruction causes the redirect. That was my original question.
Michel Krämer
A: 

Not in php but in HTTP. Yes, of course. Take yourself any HTTP sniffer, like LiveHTTPHEaders firefox addon, and watch all redirects in the realtime :)

Col. Shrapnel
A: 

What you need to do is override the function you are interested in and then do a back trace to find the file and line number.

For overriding built-in functions: http://php.net/manual/en/function.override-function.php

For generating backtrace: http://php.net/manual/en/function.debug-backtrace.php

zaf
+1  A: 

You can use XDebug and its function-trace capabilities for that. http://xdebug.org/docs/execution_trace says:

Xdebug allows you to log all function calls, including parameters and return values to a file in different formats.

Once you've found the call you can step into the code and execute it step by step. You'll need a frontend for XDebug then, e.g. netbeans.

VolkerK