PHP Header Redirecting Loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

PHP Header Redirecting Loop

Post by tamamk »

We have Solaris 10 SPARC installed, with PHP 5.2.6, Apache 2.2.9 and MySQL 5 all bundled in the CoolStack package 1.3.1 from http://cooltools.sunsource.net/coolstack/.

There are 4 domains on the server, each directing to their associated directories.

Everything is working fine except for a very strange PHP behavior, if a script is performing some task and after it is finished with the task needs to redirect back to the first script, an infinite redirection occurs.

I have made a couple of tests to get a better picture. First is a simple javascript redirecting. File send_request.html has a link to call the file recieve_request.html which has the simple javascript:

Code: Select all

 
<script type="text/javascript">
window.location = "send_request.html"
</script>
 
and everything is working fine and as expected.

A similar test was made for the PHP case. recieve_request.php has the following:

Code: Select all

 
<?php
 
  $redirect = "send_request.php?a";
  if (isset($_SERVER['QUERY_STRING'])) {
    $redirect .= (strpos($redirect, '?')) ? "&" : "?";
    $redirect .= $_SERVER['QUERY_STRING'];
  }
 header(sprintf("Location: %s", $redirect));
 
?>
 
After a few requests sent from the send_request.php, and halting for 3 seconds, then sending another request, we end up in an infinite loop.

Checking the server logs did not show anything, even at the time of running the scripts.

Any suggestions would be much appreciated!
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PHP Header Redirecting Loop

Post by it2051229 »

on your javascript the "send_html.html" is an html
then in your php the "send_php.php" is a php file.. was that a typo?
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

Nope.

I think I didn't explain it well.

There are 2 sets of files:

send_request.html
receive_request.html

and

send_request.php
receive_request.php

send_request.php and send_request.html eventually only contain simple basic links to their relevant receiving files, just to distinguish what I'm working with.

Each receive file sends back to their relative sending file. html to html, php to php.

Thanks,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

Can you show your redirect code from the scripts affected? This is usually a case of twisted logic somewhere that redirects back to a page that sends you to the redirecter.
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

Sure, though I don't think it is the case here...

send_request.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<a href="recieve_request.php">Redirect</a>
</body>
</html>

receive_request.php

Code: Select all

<?php
 
  $redirect = "send_request.php?a";
  if (isset($_SERVER['QUERY_STRING'])) {
    $redirect .= (strpos($redirect, '?')) ? "&" : "?";
    $redirect .= $_SERVER['QUERY_STRING'];
  }
 header(sprintf("Location: %s", $redirect));
 
?>
Further though, and to make it more interesting, there are 2 instances of Apache+PHP installed on the server. The first with Apache and PHP compiled from scratch, and the second is the Coolstack package.

The first one, when enabled, doesn't have this strange PHP behavior, but for some reason the administrator is having problems adding the GD library to it...and hence reserving for the second choice, installing the Coolstack package, which had everything installed nicely, except for the PHP behavior. At first, we had Coolstack version 1.3, and now it is 1.3.1.

Thanks,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

So your send request page has no PHP code in it at all? And no javascript in it at all? And when you get to receive_request and are redirected to send_request you are then redirected back to receive_request and the loop just continues?

How in the bloody hell would a page that has no redirect capabilities redirect you to anywhere? :evil: :banghead: Now I am intrigued.
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

:)

I don't think send_request is redirecting back, something just keeps it getting stuck at the receive_request and the header function keeps getting called. It is as if the script is not exiting, but even so, there is no loop ANYWHERE! Could it be something with the configuration of how coolstack is packaged? Because for one thing for sure, I know this problem does not exist with the other built-from-scratch instance of Apache+PHP....

Here is the Apache access_log, error_log doesn't report anything:

Code: Select all

xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:46 +0300] "GET /qucp/send_request.php HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:47 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:47 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:48 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:48 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:49 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 200 351
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/recieve_request.php HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a&a&a&a&a& HTTP/1.1" 302 -
xxx.xxx.xxx.xxx - - [26/Aug/2008:08:43:51 +0300] "GET /qucp/send_request.php?a&a&a&a&a&a&a&a&a&a& HTTP/1.1" 302 -
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

You can see it stacking up the redirect URL. I would guess this has to do with whatever app you have running as your server. I say turn it off and push everything through your regular apache instance to see if there is a change.
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

I have done that before but that is another different problem by itself (GD library not getting installed properly). The main difference though, this redirecting behavior does not exist when running it through the regular Apache instance.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

I wouldn't scrap the regular Apache install just because GD is not installing right. I would rather spend time on getting GD to install as opposed to put up with a server telling my apps to do something that I have no control over. That is just my two cents.

Anyhow I am not sure at all how to help you on this. It sounds like your web server is not handling something appropriately and unendingly redirecting things that are not even asking for it. Do you have any rewrite rules in place or fowarding rules in place on the modified Apache instance that might be doing this?
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

Hey Everah,

Thanks a bunch for looking into this with me.

Yes, absolutely wouldn't take the regular instance out...yet maybe. I will just have to work on it....To me it just doesn't make sense GD is working on one instance and not on the other...it must work, or, I have got to get it to work in a couple of days...otherwise the project launch is hindered....

For the sake of reference on this issue and to answer your question, no the http does not have any special redirection...

Thanks for the try anyway, appreciated,

Best,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

Perhaps we can help with the GD installation issue then. What is the problem with it loading? What is your environment?
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

I need to check on that one.

See I have experience compiling packages on Fedora, Ubuntu, and Solaris. But my position in the organization is not an admin, till today, when the director announced I could take charge of the new webserver after the normal admin declared all hopes for it and not wanting to spend more time on it since he is leaving the organization for good in a couple of days, and it is my project that needs this server. Story of my life...

I'm hating how it is done but this is my project and I cant let someone make it fail. So...RIGHT NOW, I'm duplicating the environment at home, and at work on different machines with the same system specs to see where the problem is, and then I will work on the prod. If I will need help I will be posting it here for sure.

Many thanks for your support,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Header Redirecting Loop

Post by RobertGonzalez »

No problem. I know it is frustrating. I am a web developer, but also the manager of all of our MySQL database servers, web servers and hardware for those setups.

If there is anything we can do to help be sure to post back. I am certain someone else can learn from this too.
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Header Redirecting Loop

Post by tamamk »

Just to finalize this strange PHP issue, unfortunately it won, and I had to rebuild it. But, at leas this time with connectors to LDAP and Oracle and all the rest of the extensions working fine.
Post Reply