Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 8th, 2006, 1:44 PM   #1
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
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.
leo1502 is offline   Reply With Quote
Old Aug 8th, 2006, 2:15 PM   #2
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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;

Last edited by Sane; Aug 8th, 2006 at 2:27 PM.
Sane is offline   Reply With Quote
Old Aug 8th, 2006, 3:49 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
header() can also be used for silent redirection
php Syntax (Toggle Plain Text)
  1. header("Location: 2ndpage.php");
Jimbo is offline   Reply With Quote
Old Aug 9th, 2006, 6:40 PM   #4
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
Thanks a lot guys.
leo1502 is offline   Reply With Quote
Old Aug 9th, 2006, 6:57 PM   #5
a thing
Unverified User
 
a thing's Avatar
 
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0 a thing is on a distinguished road
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).
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted.
Got 'Nux?—GNU/Linux and other free software support.
It's GNU/Linux, not just Linux.
a thing is offline   Reply With Quote
Old Aug 10th, 2006, 10:29 AM   #6
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
I thought if you don't have that, and only have "Content-Disposition" declared, it only works on half of the browsers.
Sane is offline   Reply With Quote
Old Sep 30th, 2006, 11:06 PM   #7
wheedwacker
Newbie
 
Join Date: Sep 2006
Posts: 2
Rep Power: 0 wheedwacker is on a distinguished road
http://us3.php.net/manual/en/function.header.php
gives a lot of information on it.
wheedwacker is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 6:41 PM
libraries matko C 1 Jan 22nd, 2006 3:12 PM
Php Postgresql Class Pizentios Show Off Your Open Source Projects 15 Jun 28th, 2005 10:55 AM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 2:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:27 PM.

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