help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



[Overview][Constants][Procedures and functions] Reference for unit 'pwumain' (#powtils_main)

SetHeader

Declaration

Source position: pwumain.pas line 125

function SetHeader(

  const name: String;

  const value: String

):Boolean;

Notes

This function allows you to override default HTTP headers (name and value), and/or add your own additional HTTP header name and value pairs which were not set by default.

This is useful for defining your own custom HTTP headers for your web programs, such as POWERED BY, CONTENT TYPE, LOCATION (refresh, redirect), and others.

The very first time you call Out, OutLn (WebWrite, WebWriteln) and similar output calls, the headers are output by the core utilities internally. (With Gzip compression on, it is delayed until the end of the program.. but ignore that.)

If you first make a call to SetHeader() before calling Out, OutLn, BufferOut, or similar output functions, the headers which you specify are overridden or updated with your new name and value pairs.

Tips

This is equivalent to the SetWebHeader function. The old name is available for backwards compatibility.

Example 1

Output whatever binary data you wish
begin
  SetHeader('Content-Type', 'image/png');
//  WebBufferOut(); // output your PNG/JPG/PDF/Excel/etc. data here
end.

Example 2

Output a PNG image from a memory stream.
uses
  Classes, pwumain;  // or dynpwu;
var
  MS : TMemoryStream;
begin
    MS := TMemoryStream.Create;
    MS.LoadFromFile('myfile.png');
    SetHeader('Content-Type', 'image/png');
    BufferOut( MS.Memory^,MS.Size );
    MS.Free;
end.

Example 3

Redirect or refresh the web browser after processing data.
begin
    // do a bunch of processing without calling webwrite 
    //...
    //...
    {.. then REDIRECT the user to a new website: }
    SetHeader('Location', 'http://some-site.com');
    Out('This text is not shown because user is sent to above URL. ');
    Out('If you want text to be shown, use a meta redirect tag instead. ');
end.
Note that you cannot call webwrite, out, outln, or similar functions before calling SetHeader, since output functions would have already sent the web headers. (If Gzip is on however, or buffering is used that will not be the case since gzip compresses the program at the end and sends all to STDOUT during finalization stages. Set all web headers before making output calls to make porting your application back and forth to gzip or no gzip easier.)

Use a meta refresh tag through HTML if you wish to display a message in the browser, or build a bunch of cgi programs that chain together and send the user to this program after a form POST/GET request.

Example 4

Set your own web header before using Webwrite, such as POWERED BY
begin
  SetHeader('X-Powered-By', 'Joe Computer Systems');
  Out('This is my web program');
end.
All example 4 does is show that your site is not POWERED BY the Powerful Web Utilities. If you have people sniffing your website they can tell if you are using PHP, PWU, ASP, Perl, etc. by checking your POWERED BY web header.

Example 5

begin
  SetHeader('Pragma', 'No-cache');
  Out('this should force program to be fresh and current');
  Out('You could also use meta tag no-cache inside html HEAD');
end.
This demands the web browser to load the page from the server (fresh) instead of from the visitors cache on his hard drive.

Any HTTP Header Can Be Set

Refer to the w3.org HTTP Specifications or similar HTTP specification websites to find out more about HTTP headers.

Some example HTTP headers are:

  Accept
  Accept-Charset
  Accept-Encoding
  Accept-Language
  Accept-Ranges
  Age
  Allow
  Authorization
  Cache-Control
  Connection
  Content-Encoding
  Content-Language
  Content-Length
  Content-Location
  Content-MD5
  Content-Range
  Content-Type
  ETag
  Expect
  Expires
  From
  Host
  If-Match
  If-Modified-Since
  If-None-Match
  If-Range
  If-Unmodified-Since
  Last-Modified
  Location
  Max-Forwards
  Pragma
  Proxy-Authenticate
  Proxy-Authorization
  Range
  Referer
  Retry-After
  Server
  TE
  Trailer
  Transfer-Encoding
  Upgrade
  User-Agent
  Vary
  Via
  Warning
  WWW-Authenticate

See Also

PutHeader





lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505