HTTP Connections - PHP

When you type a URL into the location box on your browser, the first task of the browser is to break it up into important parts, the first of which is the protocol, HTTP. Next is the name of the Web server, to which the browser makes a connection. The browser must tell the Web server which document it wants, and it does so using the HTTP protocol. Before completing the request, the browser may provide lines of extra information called headers. These headers let the server know the brand of the browser, the type of documents the browser can accept, perhaps even the URL of a referring page.

The Web server places these headers into environment variables to conform with the Common Gateway Interface (CGI). When a PHP script begins, the environment variables are converted into PHP variables. One of the most useful headers describes the brand and version of the Web browser. This header is sent by the browser as User-agent. The Web server creates an environment variable called HTTP_USER_AGENT that holds the value of the header. PHP in turn creates a variable with this same name. You can refer to it using $, just as for any other variable. If you are using Apache, you also have the option of using the getallheaders function. It returns an array of all headers exchanged between the browser and the server.

As a PHP script begins to execute, the HTTP exchange is in the stage where some headers have been sent to the browser, but no content has. This is a window of opportunity to send additional headers. You can send headers that cause the browser to ask for authentication, headers that request that the browser cache a page, or headers that redirect the browser to another URL. These are just some of the many HTTP headers you can send using the header function.

Headers are placed on a stack, which is a data structure that resembles a literal stack of dinner plates. Imagine that each plate is a header. Each new plate is placed atop the previous plate. When it's time to send the headers, they are removed from the top, one at a time. This has the effect of sending the headers to the browser in the reverse of the order in which they were added. Usually this has no effect. HTTP doesn't define any special meaning to the order of headers. However, if you send the same header twice, the later header may overwrite the value of the earlier. This means that if you try resending a header, the browser most likely will ignore it.

Once any content is sent, the opportunity to send headers is lost. This includes any text outside of PHP tags, even if it's just a linefeed. If you try to send a header after content is sent, an error message is generated. You can use the headers_sent function to test whether it's safe to add more headers to the stack, or too late. Cookies, described below, use headers, and therefore are limited in the same way.

As a script runs and sends content, the output is buffered. There is a bit of overhead to every network action, so a small amount of memory temporarily stores the information to be sent out in batches. This buffer is owned by the Web server, so PHP does not have control of it. However, you may request that the buffer be flushed—immediately sent to the browser—by using the flush function. This is most useful in long scripts. Both browsers and people have limits to how long they wait for a response, so you can let them know you're making progress by flushing the output. I've written scripts that print a single period and then flush the buffer each time through a long loop.

There are two ways a script may halt unexpectedly: when the script runs too long, and when the user clicks the stop button. By default, scripts are limited to a number of seconds specified in php.ini. This is usually 30 seconds, but you can change it. Look for the max_execution_time directive. But 30 seconds is a good setting. In case you write a script that could run forever, you want PHP to stop it. Otherwise a few errant scripts could slow your server to a crawl. For the ame reason, you usually want to allow users to be able to abort a page request.

There are times when you do want a script to run to completion, and you can instruct PHP to ignore time limits and user aborts. The set_time_limit function resets PHP's timer.

Instead of just letting a script run, you may wish it to halt, but deal with the reason it halted with special code. To do this, you must first tell PHP to execute a special function whenever a script ends. This is done with register_shutdown_function. This function will execute regardless of why a script ended. It even executes when the script ends normally. You can test for the reason with two functions: connection_aborted and connection_timeout.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics