PHP: Information on a Web Page
In
the first part of this article, we saw how to
parse part of a URL, and the parameters involved in the case where one
deals with a string.
This part shows how to get the same information, including obtaining data from a form to pass to a PHP script.
Comparison with parse_url
Data on the script or PHP page are obtained with the predefined variable $_SERVER and a list of keys corresponding to the information sought, which are given in the table below.
Take note that the domain name is given directly by the user. In our example, it is www.scriptol.com.
| Data |
parse_url
|
Keys for $_SERVER
|
| Full URL |
__FILE__
|
domain + REQUEST_URI
|
| Protocol |
scheme
|
SERVER_PROTOCOL
|
| Domain |
host
|
user constant
|
| Directory and file |
path
|
SCRIPT_NAME
|
| Anchor in the page |
fragment
|
-
|
| Parameters |
query
|
QUERY_STRING
|
| Port |
-
|
-
|
| Login |
user
|
Treated separately
|
| Password |
pass
|
Treated separately
|
Differences:
- The parse_url function provides the generic name of the protocol while the system variable includes the version, for example in the form HTTP/1.1.
Using the predefined variable
Example:
$protocol = $_SERVER[SERVER_PROTOCOL];
The list of keys is provided in the PHP manual under the heading: "predefined variables."
Retrieving data from an HTML form
Example:
$parameters = $_SERVER[QUERY_STRING]; parse_str($parameters, $data); print_r($data);
$data is an associative array, where keys are variables given as parameters, and values, the content of these variables.
See the first part of the article for more details.