[Overview][Constants][Procedures and functions] | Reference for unit 'pwumain' (#powtils_main) |
Source position: pwumain.pas line 125
function SetHeader( |
const name: String; |
const value: String |
):Boolean; |
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.
begin SetHeader('Content-Type', 'image/png'); // WebBufferOut(); // output your PNG/JPG/PDF/Excel/etc. data here end.
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.
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.
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.
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.
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