2

I am learning HTTP and web programming and I was surprised to find out that you can set HTTP headers using php. I thought PHP was used to generate dynamic HTML web pages and create applications. It seems strange to me that PHP can set headers that are sent by the web server (e.g. Apache). I know PHP interpreter reads the PHP file and generates an output usually in the form of HTML.

Does this process work with pipes? In my opinion the apache server has to be able to receive commands from the PHP interpreter or it has to be able to interpret PHP functions itself. They are separate processes I think.

What mechanism is used by PHP to set headers to the web server application (httpd or Apache or something else)

Do all web servers support receiving and setting headers that are received via PHP?

Is it possible to set HTTP headers with all backend languages?

I searched through the website and I did not find an aswer to my question.

More specifically I want to know what command can apache or other web server send to PHP.exe application or PHP-CGI.exe application to receive other information besides the outputted HTML file.

13
  • 2
    Apache2 is usually set up with a PHP module - libapache2-mod-php5 - which is probably why it is possible. Commented May 6, 2016 at 14:27
  • @ Antony D'Andrea Does this mean apache and PHP run within the same process? Isn't the PHP interpreter a separate process running in the OS? Commented May 6, 2016 at 14:29
  • I don't understand, is it really that surprising? If PHP can generate dynamic content based on a few instructions in a .php file even though the webserver is still the one serving the content, why can't it set headers? Oh and the PHP interpreter is not a separate process, it's like an extra binary package that the webserver calls. Commented May 6, 2016 at 14:30
  • Headers are sent to the browser, they also have to be sent before Apache starts retuning any part of the web page as headers must arrive to the browser first before any actual page content Commented May 6, 2016 at 14:36
  • Remember, when Apache see's a <php tag the whole page is sent to PHP for interpretation and execution. Once PHP is finished adding stuff to the page PHP passes the finished page is passed back to Apache so it can deliver it to the browser Commented May 6, 2016 at 14:38

3 Answers 3

1

Indeed an interesting question. Using command-line tools, I can only access the produced HTML output. Hence php -r"header('Location: http://someurl.com');" will produce nothing from command line.

When I would look at my setup with IIS (not Apache) though, I see that IIS is using PHP-CGI.exe to communicate with PHP. Looking at the optional arguments of PHP-CGI.exe, I see -b can be used to set a Bind Path for external FASTCGI Server mode. I guess in this server mode there will be room to communicate header information separate from produced HTML.

I don't know the exact details of the FASTCGI protocols to go more indepth. But I guess this is what you wanted to know.

EDIT:

When googling about this, I came upon this thread: How does PHP interface with Apache?

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

1 Comment

Thank you for understanding my question and taking time to answer. I will still wait to see if somebody can give a more in-depth answer
0

Sending headers from PHP is very useful, for example, you can send headers that force the browser to download a file vs. displaying it on screen (for file scripts, reports that output CSV), controlling cache headers and performing page redirection.

As stated in one of the comments, PHP being a module of Apache in many installs, it sends headers directly through that. Otherwise, the headers would be sent via CGI / FastCGI, or PHP-FPM for nginx.

What you're thinking of more or less is of a templating engine, which PHP performs well at, but PHP has other functions that would normally not be seen in a template engine, such as opening sockets, handling files, and so on.

Any backend language I've had experience with has support for sending HTTP headers, and I would consider any web-oriented back-end language incomplete without this ability.

2 Comments

I understand this. My question was how does apache knows that the certain PHP file that the module has interpreted contains a directive for it to send a header?
@yoyo_fun Well apache is getting output from PHP, so there must be a mechanism for that. What's to say the mechanism for sending headers isn't the same? (hint: there's a reason you can't send headers when output has already been sent)
0

header('Location: http://www.google.com/');

In this way we can set header in php

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.