trace infinite loop redirection in apache
I have a PHP script causing a infinite redirection loop and i don't know how to detect it. is there any way to detect this? can i add any command to my .htaccess 开发者_开发百科to write redirections into a log?
thanks in advance,
Debugging in PHP in simple way is actually a cumbersome effort. What you will need is you should provide some "echo" statement (outputting a dummy text) after each line, followed by the "exit" command statement.
Then the redirection loop will not work because "exit" is making the program counter stoppable, and o you can now debug sequentially where you are going wrong.
As regards to the ".htaccess" command, you will not need any such thing. Please check in your root folder for the "error.log" file, where you have hosted your website's index.php file. If you open this file in some text editor (like Wordpad), you will see all the list of errors, along with date & time.
EDIT (providing with one example):-
Let's say you have one page like the following:-
<?php
session_start();
include_once("includes/header.php");
$var1 = 'blah blah';
// some code
$var2 = 'some more blah blah';
// some code again
// may be some more code
include_once("includes/footer.php");
?>
Now change the code to debug in this way:-
<?php
session_start();
echo 'test 1';
exit;
// other code of yours comes after this line
?>
If it works, change again to:-
<?php
session_start();
include_once("includes/header.php");
echo 'test 2';
exit;
// other code of yours comes after this line
?>
Now if it works, that means you have no problem in the "header.php" file, otherwise you are having some problem in that included file & you will need to start debugging in the same way in that "header.php" file.
Hope it helps.
Change instances of
header ("Location: foo.php");
to
//header ("Location: foo.php");
echo "redirecting to foo.php on line ## of bar.php";
exit;
(Most likely foo = bar, but you could have a loop containing more than one page...)
I'm currently struggling with infinite redirection myself. Due to complexity of the aplication and afterhours programming cycle not all redirections have been made the right way.
If you've set session at the beginning (let say autloader file) you cannot simply check if session loops itself.
My advice is that you write variables redirection to an external file (i.e. using file_put_contents or fwrite) so that you can follow execution even if your browser detects some misconception. Writing to an external file gives you an oportunity to follow execution even though you cannot see any echo on screen.
Infinite redirections usually come from unnecessary complication while writing code and misunderstanding in forming project structure.
Good luck!
精彩评论