PHP header function - redirection does not work using header("Location: url")

Topics: faq
Apr 5, 2013 at 7:50 AM
Edited Apr 5, 2013 at 7:51 AM
Good day everyone.

We are using this project to implement a local testing environment for websites and notably PHP pages, and we got it working rather well. Thank you for the powerful solution. But we've stumbled on the fact that the function
header('Location: http://google.com');
does not result in redirecting. The function works it seems, browser receives new headers, including Location, but no redirection happens. I simplified the code to the following (to avoid any possible output etc., and there are no error reports whatsoever)
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

// header("Content-type: image/png;"); // this works, for example
header('Location: http://www.google.com'); // this doesn't, though it works with easyPHP etc.

?>
I didn't find any posts here related to this. The help would be very appreciated.

Sincerely
D
Coordinator
Apr 5, 2013 at 8:04 AM
Which HTTP status code is returned?


Apr 5, 2013 at 8:06 AM
Edited Apr 5, 2013 at 8:07 AM
If we are talking about the same thing, it's
"200 Made by Jonas Gauffin"
it's what I see in FireBug" in status column. It is the only visible difference I see compared to other servers executing the same page..

EDIT easyPHP, for example, returns "302 Found"
Coordinator
Apr 5, 2013 at 8:09 AM
Ok. It needs to be 302 to tell that it's a redirect. I guess that the PHP module needs to check if the location header has been set and if so change the status code to 302.


Coordinator
Apr 5, 2013 at 8:10 AM
You can also try to change it in php (just to validate that it works then): http://stackoverflow.com/a/6179761/70386


Apr 5, 2013 at 8:20 AM
I've already tied to set the Status header, and I've tried the code from StackOverflow, as follows:
header("Status: 302"); // for fast cgi
header($_SERVER['SERVER_PROTOCOL'] . " 302");
and
header("Status: 302 Found"); // for fast cgi
header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
and in FireBug I see that Status header is sent, but "Status of received responces" (Status column in Network output) is still "200 Made by Jonas Gauffin".

Could you please hint me where to make the changes in the source code?

Thank you for your support
D
Apr 5, 2013 at 8:26 AM
P.S. I think I found it, in Response.cs Response() method. I will modify and post here the results..
Developer
Apr 5, 2013 at 8:58 AM
Edited Apr 5, 2013 at 9:03 AM
If you are using the code from http://wo80.hostingsociety.com/p/httpserver.addons :

Open CgiHandler.cs and add the 302 to the GetStatusCode method:
private static HttpStatusCode GetStatusCode(string value)
{
    if (value.StartsWith("404"))
    {
        return HttpStatusCode.NotFound;
    }
    else if (value.StartsWith("401"))
    {
        return HttpStatusCode.Unauthorized;
    }
    else if (value.StartsWith("302"))
    {
        return HttpStatusCode.Redirect;
    }

    return HttpStatusCode.OK;
}
This should fix the problem.

EDIT: just saw that the online version is not up to date, so the above won't work. Will be updated soon...
Apr 5, 2013 at 9:08 AM
I think we use another version; there's no such function in CgiHeaders.cs, but there's ParseCgiHeaders() (with several ToDo comments), I will try to look into it..
Apr 5, 2013 at 9:31 AM
Edited Apr 5, 2013 at 9:32 AM
Ok I think I made it work.

We use the version from here:
http://wo80.hostingsociety.com/p/httpserver.addons/#download
downloaded somewhere in March 2013.

I added the code to CgiHeaders.cs ParseCgiHeaders() function (see code between // DD START and // DD END comments):
        public static void ParseCgiHeaders(ref string cgiOutput, IResponse response)
        {
            // TODO: Make this more robust (are we really stripping headers???)
            int index = cgiOutput.IndexOf("\r\n\r\n");

            if (index != -1)
            {
                string header = cgiOutput.Substring(0, index + 2);
                cgiOutput = cgiOutput.Substring(index + 2);

                int end = header.IndexOf("\r\n");

                while (end != -1)
                {
                    string line = header.Substring(0, end);
                    header = header.Substring(end + 2);

                    int colonIndex = line.IndexOf(":");
                    if (colonIndex <= 1)
                        break;

                    string val = line.Substring(colonIndex + 1).Trim();
                    string name = line.Substring(0, colonIndex).Trim();

                    // TODO !!!
                    if (name.ToLowerInvariant() == "content-type")
                    {
                        response.ContentType.Value = val;
                    }
                    // DD START fix redirecion by Location header:
                    else if (name.ToLowerInvariant() == "location")
                    {
                        response.Redirect(val);
                    }
                    // DD END fix redirecion by Location header
                    else
                    {
                        response.Add(new StringHeader(name, val));
                    }

                    end = header.IndexOf("\r\n");
                }
            }

            cgiOutput = cgiOutput.Trim();
        }
Thank you everyone, I would have had heavy difficulties finding it on my own..

Sincerely
D
Developer
Apr 5, 2013 at 10:58 AM
I updated the code. There are some breaking changes, so also have a look at the example on the webpage.

http://wo80.hostingsociety.com/p/httpserver.addons/

Regarding your problem: the code for setting the CGI headers is now in CgiModule.cs in the HttpServer.Addons.Modules namespace.
Apr 5, 2013 at 11:31 AM
Edited Apr 5, 2013 at 12:02 PM
Thank you. Have you also fixed an issue with missing POST data? I had to fix it locally, it's more or less along the lines of this thread:
https://webserver.codeplex.com/discussions/229392
(EDIT: our version may be obsolete compared to latest releases)
Apr 5, 2013 at 11:46 AM
Edited Apr 5, 2013 at 11:46 AM
P.S. Sorry, POST data fixes were done in HttpServer itself, not in Addons, so it's not related to your last post.
Developer
Apr 5, 2013 at 11:54 AM
Edited Apr 5, 2013 at 11:55 AM
DELETED