Page 1 of 1

Redirect after HTTPResponse send.

Posted: Wed Dec 03, 2008 2:07 pm
by ganga
Hi,
How can i redirect to a different page after my download started.
Here is my code.

HttpResponse::setCache(true);
HttpResponse::setContentType('application/octet-stream');
HttpResponse::setContentDisposition("abc.exe", false);
HttpResponse::setFile(realpath('./abc/abc.exe'));
HttpResponse::send();
ob_flush();
flush();
sleep(1);
header("Location: http://yahoo.com"); // here i want to redirect to a page after download started. But this is not working.

How can i do this?

Thanks

Re: Redirect after HTTPResponse send.

Posted: Thu Dec 04, 2008 11:55 pm
by Chris Corbyn
Redirects work based on HTTP headers themselves.

A full HTTP response looks something like:

Code: Select all

HTTP/1.0 200 OK
Content-Type: text/html; charset="utf-8"
Content-Length: 12
 
Hello World!
 
If the browser processes the headers before it displays the content. Now, in order for a redirect to work you need to set a "Location" header.

Code: Select all

HTTP/1.0 302 Moved
Location: http://site.tld/some-other-place/
 
 
As you can probably see from that concept then, if you've already sent your headers, it's not possible to send the Location header and do the redirect. It would just be a line within the body of the page and would not be interpreted by the browser.

You may possibly be able to cheat by using a multipart document. You'd have to make some drastic coding changes for this to work however since your document needs to be nested inside a parent document:

Code: Select all

HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary="_-_something"
 
--_-_something
Content-Type: text/html; charset="utf-8"
Content-Length: 12
 
Hello World!
--_-_something
Status: 302
Location: http://site.tld/some-other-place/
 
--_-_something--
 
The document consists of two smaller HTTP documents inside itself. I'm not sure the redirect would be interpreted however.

Better off figuring out if you need to redirect before you commit any response to the client ;)

Re: Redirect after HTTPResponse send.

Posted: Fri Dec 05, 2008 12:10 am
by alex.barylski
Holy...your alive. I haven't seen you (CC) or K-Hugs in ages and all of a sudden you both show up...what nerve :P

Just teasing :)