Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   header() function, can anyone explain (http://www.programmingforums.org/showthread.php?t=10999)

leo1502 Aug 8th, 2006 1:44 PM

header() function, can anyone explain
 
I'm seeing a lot of the header() function.
Can anyone tell me what exactly are raw-headers and how do they work?
Thanks a lot.

Sane Aug 8th, 2006 2:15 PM

When the server returns the page that the client requests, it also attaches along hidden information that the browser uses (but doesn't necessarily let you see).

The header information can be used to tell your browser to use the cached page, it can be used to raise a 404 (missing page), a 505 (server error), it's used to tell the browser what server the site is running on.

It can also be used to differentiate data from html (Content-type: text/html) and an image (Content-type: image/jpeg). Or even trigger file downloads (Content-type: application/x-download).

This is all part of what makes up the Hyper Text Transfer Protocal (HTTP). Basically, it's what helps your browser interpret precisely what the server wants you to see.

With use of the header() function in PHP, you can modify the raw header by simply putting the information in to the funtion call. Without use of the header() function, the default header information is used according to the PHP default settings. So doing something like "header('Content-type: image/jpeg')", will modify the default content-type attribute to the newly specified format.

For further understanding, here's an example of a PHP download script. It has some undeclared variables, but it's simple to work in to something usable.

:

  $stream = fopen($fname, 'rb');
  $contents = fread($stream, filesize($fname));
  fclose($stream);

  header('Content-Type: application/x-download');
  header('Content-Disposition: attachment; filename="'.$fname.'"');

  echo $contents;


Jimbo Aug 8th, 2006 3:49 PM

header() can also be used for silent redirection
:

  1. header("Location: 2ndpage.php");


leo1502 Aug 9th, 2006 6:40 PM

Thanks a lot guys.

a thing Aug 9th, 2006 6:57 PM

Sane: application/x-download is silly. It doesn't tell the browser what the file really is, so it can't decide what to do with it (like open in an external application).

Sane Aug 10th, 2006 10:29 AM

I thought if you don't have that, and only have "Content-Disposition" declared, it only works on half of the browsers.

wheedwacker Sep 30th, 2006 11:06 PM

http://us3.php.net/manual/en/function.header.php
gives a lot of information on it.


All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC