0

can I call two consecutive headers in my php code ?

header("Location: http://" . $_SERVER["HTTP_HOST"] . "/$file"); //download file (http get)
header("Location: http://" . $_SERVER["HTTP_HOST"] . "/$page"); //redirect to page

The first header function redirect to a url to download a file, while the second header forwards the customer to the next page.

Currently, if I list both headers in sequence, the first header is ignored.

thanks

3 Answers 3

1

can I call two consecutive headers in my php code ?

You can emit two headers but only one is going to be interpreted by the browser, so no, it's not really possible.

I would consider serving the file download in a link with target='_blank' to keep the original window intact.

Sign up to request clarification or add additional context in comments.

2 Comments

mhm, is there any other way to do it, without opening an empty browser window ? I actually have to invoke a library function to generate a pdf and send an email. It should be invisible to the user
@Patrick why not invoke the function in the file you use to serve the download in? Is that a PHP file as well? I would add some more info to make it clearer.
0

As soon as you send that first header, the user is redirected and doesn't see anything else. What you want to do is redirect the user to a page and begin a file download on that page.

1 Comment

Sending a header does not stop execution of the script. The second header will also be sent. any code below the header call will also run unless you call exit() or die(). Sometimes browsers will quickly redirect, other times they will show whatever else happens on the first page until the second page returns.
0

I would suggest looking here:

http://php.net/manual/en/function.header.php

There were a lot of examples and comments on this page. You might find some of that information interesting in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.