Home >Backend Development >PHP Tutorial >PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI: What\'s the Difference?

PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI: What\'s the Difference?

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 15:48:02198browse

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

  • PHP_SELF: /test.php/foo/bar
  • SCRIPT_NAME: /test.php

Example 2:
http://example.com/test.php?foo=bar

  • SCRIPT_NAME: /test.php
  • REQUEST_URI: /test.php?foo=bar

Example 3:
http://example.com/test.php (with mod_rewrite redirect)

  • REQUEST_URI: /test.php
  • SCRIPT_NAME: /test2.php

Example 4:
Custom error page in IIS:
http://example.com/test.php

  • SCRIPT_NAME: /404error.php
  • REQUEST_URI: /404error.php?404;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn