1

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,

FidoBoy
  • 21
  • 2
  • 4
  • There should already be a log, it's called an error log. It will usually tell you what file caused the loop and at what time. – animuson Jul 03 '10 at 04:30
  • Could you possible update your question with your htaccess in case ? – Prix Jul 03 '10 at 04:30
  • @animuson: there is nothing in the error log if the redirect is caused by a script (and I don't think there would be for htaccess redirects either). Which makes sense - the error is raised on the client side, the server does not know about it. – Tgr Mar 13 '12 at 11:13
  • Use error_log function – Kzqai Apr 16 '13 at 17:06
  • Related: http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules/9261963#9261963 – Highly Irregular Mar 05 '14 at 01:28

3 Answers3

1

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
  • 7,955
  • 13
  • 49
  • 92
  • 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 Jul 03 '10 at 09:34
  • But I think this is the best solution at hand, if you want to understand the problem lying in your script. – Knowledge Craving Jul 03 '10 at 16:08
0

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!

jakubplus
  • 307
  • 4
  • 17
0

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
  • 6,694
  • 1
  • 25
  • 36