Home >Backend Development >PHP Tutorial >PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI: What\'s the Difference?
PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI: Understanding the Differences
When developing PHP applications, it's crucial to understand the difference between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI. These server variables provide information about the current script's address and can be useful for navigation and URL rewriting.
PHP_SELF typically contains the full URI of the current script, including the script name and any path information. However, it can be different from SCRIPT_NAME in cases where the request is in the form of http://example.com/test.php/foo/bar.
PATH_INFO contains the path information at the end of the request URI. However, it's only populated when the request URI is in the form mentioned above.
SCRIPT_NAME represents the name of the current script, excluding any path information. It's usually the same as PHP_SELF unless there is a / in the URI.
REQUEST_URI contains the complete request URI, including the path, query string, and anchor. It differs from PHP_SELF and SCRIPT_NAME when a query string is present or when server-side redirection is used.
To illustrate the differences, here are some practical examples:
Example 1:
http://example.com/test.php/foo/bar
Example 2:
http://example.com/test.php?foo=bar
Example 3:
http://example.com/test.php (with mod_rewrite redirect)
Example 4:
Custom error page in IIS:
http://example.com/test.php
By understanding these differences, you can effectively utilize the appropriate server variable to handle navigation, URL rewriting, and error handling in your PHP applications.
The above is the detailed content of PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI: What\'s the Difference?. For more information, please follow other related articles on the PHP Chinese website!