Home  >  Article  >  Backend Development  >  What are the key differences between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI?

What are the key differences between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-02 07:28:02810browse

What are the key differences between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI?

Understanding PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI**

When developing PHP applications, it's essential to understand the differences between these server variables. Here's a breakdown of their uses:

PHP_SELF

  • Normally points to the current script file, e.g. /index.php/faq/whatever
  • In CodeIgniter, it's always index.php, as requests are routed to the main controller.

PATH_INFO

  • Contains the path information after the script name (not including query parameters), e.g. /faq/whatever
  • Useful for accessing specific sections of a URI.

SCRIPT_NAME

  • Points to the actual PHP script file being executed, e.g. /test.php
  • Unlike PHP_SELF, it doesn't include any path information or query parameters.

REQUEST_URI

  • Represents the full request URI, including path information and query parameters, e.g. /test.php?foo=bar
  • Useful for determining the complete URL without accessing the server configuration.

Practical Differences

Example 1: PHP_SELF vs SCRIPT_NAME

If the requested URL is in the form /test.php/foo/bar:

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

Example 2: REQUEST_URI vs SCRIPT_NAME

If a non-empty query string is entered in the URL:

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

Example 3: REQUEST_URI vs SCRIPT_NAME with Apache Mod Rewrite

With server-side redirection, e.g., /test.php is rewritten to /test2.php:

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

Example 4: REQUEST_URI vs SCRIPT_NAME with Error Handling

  • With Apache mod_rewrite, a 404 error in /test.php might result in:

    • REQUEST_URI: /test.php
    • SCRIPT_NAME: /404error.php

The choice of which server variable to use depends on the specific application requirements. For example, if you need to access path information without query parameters, PATH_INFO would be suitable. If you want the full request URI, REQUEST_URI provides this information.

The above is the detailed content of What are the key differences between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI?. 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