Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

How to get current PHP page name

Getting the current page name in PHP is a way to determine the name of the current script being executed by the web server. This can be useful for various purposes, such as creating navigation menus, performing conditional logic, or logging user activity.

The current page name is typically obtained from the $_SERVER superglobal, which is an array containing information about the web server, headers, and the request. By using a combination of the $_SERVER superglobal and some PHP functions, it is possible to extract the current page name.

Some of the common ways to get the current page name in PHP are:

  • Using the $_SERVER[‘PHP_SELF’] variable which contains the path of the currently executing script, relative to the document root.
  • Using the $_SERVER[‘SCRIPT_NAME’] variable which contains the path of the currently executing script, relative to the document root.
  • Using the $_SERVER[‘REQUEST_URI’] variable which contains the URI which was given in order to access this page.
  • Using the constant FILE which contains the full path and filename of the current file.

Once you have the current page name, you can use it to perform various tasks such as:

  • Creating navigation menus that highlight the current page.
  • Performing conditional logic based on the current page.
  • Tracking user activity by logging the current page name.
  • Create dynamic routing in your application.

It’s important to note that these methods might not work correctly if the current page is being rewritten by the web server. In this case, it’s better to use the URL rewriting techniques in the web server configuration.


In PHP, there are several ways to get the current page name. Here are a few examples:

  1. Using the $_SERVER superglobal:
$currentPage = basename($_SERVER['PHP_SELF']);

This will get the current page name from the PHP_SELF key in the $_SERVER superglobal, and then use the basename() function to remove any directory information.

  1. Using the $_SERVER superglobal and REQUEST_URI
$currentPage = basename($_SERVER['REQUEST_URI']);

This will get the current page name from the REQUEST_URI key in the $_SERVER superglobal, and then use the basename() function to remove any directory information.

  1. Using the $_SERVER superglobal and SCRIPT_NAME
$currentPage = basename($_SERVER['SCRIPT_NAME']);

This will get the current page name from the SCRIPT_NAME key in the $_SERVER superglobal, and then use the basename() function to remove any directory information.

It’s important to note that these methods might not work correctly if the current page is being rewritten by the web server. In this case, it’s better to use the URL rewriting techniques in the web server configuration.

You can also use the constant FILE to get the current file name, this constant contains the full path and filename of the current file.

$currentPage = basename(__FILE__);

Please note that the extension of the current file will also be included in the result.


Leave a reply