views:

36

answers:

2

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,

+1  A: 

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.

Knowledge Craving
well, actually i have literally hundreds of php scripts; sincerely, i can't use this technique for every script... :( I've readed something about redirect logs for rewrite engine in apache, but i don't know how to use it...
FidoBoy
But I think this is the best solution at hand, if you want to understand the problem lying in your script.
Knowledge Craving
A: 

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...)

grossvogel