HTTP Headers PHP

HTTP headers are special commands sent between the browser and Web server before the browser receives any content. Some of the headers let the server know which file the browser wants. Others may instruct the browser about the type of file it will soon be sent. To learn more about headers, refer to the HTTP specification that was originally described in RFC 1945. It and other documents may be found at the W3C site, which has a section devoted.

boolean header(string http_header)
The header function sends an HTTP header to the browser. It must be called before any output is sent to the browser, inside or outside PHP tags. Many different kinds of headers may be sent. Perhaps the most common is a location header, which redirects the browser to another URI.

Each time you call header, it is pushed onto a stack. If you are unfamiliar with the concept of a stack, think of it as a list of items placed one on top of another. When your script gets to the point of sending content to the browser, headers are pulled from the stack one at a time. This means headers are sent to the browser in reverse order. Headers are also used to send cookies, but PHP's setcookie function is better suited for this purpose.

One common trick the header function provides is sending a user to another page, as demonstrated in the example below. Another is to force the browser to either download the file or display it in an OLE container. This isdone by setting the Content-typeheader, which PHP defaults to text/html. Sending a value of application/octetstream will cause most browsers to prompt the user for where to save the file. You can also use other MIME types to get the browser to run a helper application. For example, if you use application/vnd.ms-excel, a Windows machine with Microsoft Excel installed will launch Excel in an OLE container inside the browser window. In this case you don't need to send an actual Excel file. A simple tab-delimited file will be interpreted correctly.

boolean setcookie(string name, string value, integer expire,string path, string domain, integer secure)
Use setcookie to send a cookie to the browser. Cookies are sent as headers during an HTTP connection. Since cookie headers are more complex than other headers, it is nice to have a function specifically for sending cookies. Keep in mind that all headers must be sent prior to any content. Also, calling setcookie does not create a PHP variable until the cookie is sent back by the browser on the next page load.

If setcookie is called with only the name argument, the cookie will be deleted from the browser's cookie database. Otherwise, a cookie will be created on the client browser with the name and value given.

The optional expire argument sets a time when the cookie will automatically be deleted by the browser. This takes the form of seconds since January 1, 1970. PHP converts this into Greenwich Mean Time and the proper form for the Set-Cookie header. If the expire argument is omitted, The browser will delete the cookie when the session ends. Usually this means when the browser application is shut down.

The path and domain arguments are used by the browser to determine whether to send the cookie. The hostname of the Web server is compared to the domain. If it is left empty, the complete hostname of the server setting the cookie is used. The path is matched against the beginning of the path onthe server to the document. The cookie specification requires that domains contain two periods. This is to prevent scripts that get sent to every top-level domain (.com, .edu, .net). It also prevents a domain value of leonatkinson.com. Just remember to add a leading dot.

The secure argument is used to tell the browser to send the cookie only over secure connections which use Secure Socket Layers. Use a value of 1 to denote a secure cookie. Like other headers, those created by the setcookie function are pushed onto a stack, which causes them to be sent in reverse order. If you set the same cookie more than once, the first call to setcookie will be executed last. Most likely, this isn't what you intend.

Keep track of the value you intend to set as the value of the cookie and call setcookie once. Netscape, which developed cookies, offers more information about them in a document titled Persistent Client State: HTTP Cookies. How do you know if a browser accepts your cookie? The only way is to send one and test that it is returned on the next page request.

/*
** mark this site as being visited
** for the next 24 hours
*/
setcookie ("HasVisitedLast24Hours", "Yes", time()+86400;
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics