How to get current PHP page name

How to get current PHP page name

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Get Current PHP Page Name Without Query Strings

When building a conditional UI, you often hit a wall where GET parameters aren't enough. A common scenario involves hiding or showing elements based on the URL itself rather than query strings. If your file is named demo.php and you need to render different markup depending on that specific path, checking $_GET['name'] won't cut it because no such parameter exists in the address bar. You need to inspect the script name directly.

The most straightforward approach for vanilla PHP relies on the $_SERVER superglobal array. Specifically, the $_SERVER['SCRIPT_NAME'] variable contains the path of the currently executing script relative to the document root. For a file located at /public/demo.php, this variable will typically return the string demo.php.

You can implement your conditional logic immediately:

<?php
$currentFile = basename($_SERVER['SCRIPT_NAME']);

if ($currentFile === 'demo.php') {
    // Hide button or show alternative content
} else {
    // Show standard content
}
?>

Using basename() strips any directory traversal attempts or relative path prefixes that might slip into the raw SCRIPT_NAME, ensuring you are comparing strictly against the filename. This keeps your logic robust even if your application is installed in a subdirectory like /app/demo.php.

While $_SERVER works, it can be brittle in complex deployment environments. If you use mod_rewrite to mask the actual file extension or route requests through a virtual host, SCRIPT_NAME might return an unexpected path string that doesn't match your local filename. In those cases, relying on the raw superglobal requires extra validation logic to normalize the path before comparison.

For projects using PHP frameworks like Laravel, the approach shifts slightly due to their internal routing layer. Frameworks often decouple the physical file structure from the logical URL. In Laravel, you should utilize request()->route()->getName() if your element visibility depends on the route definition rather than the physical script name. This ties the UI logic to your routes file, making it easier to maintain as you refactor controllers without worrying about server configuration changes.

However, there is a significant trade-off when deciding between checking the physical filename versus the route name. If you hardcode checks against demo.php, you are coupling your application's logic to its filesystem structure. Moving that file from demo.php to index.php breaks the conditional unless you update every PHP file containing the check. Conversely, checking routes or using a dedicated configuration constant decouples logic from location but adds a slight overhead of resolving the route name in the global scope.

In high-performance applications where minimizing function calls is critical, accessing $_SERVER['SCRIPT_NAME'] is generally faster than invoking framework-specific methods to resolve route names. But speed shouldn't come at the cost of maintainability. If your team frequently renames files or restructures directories, a dependency on the physical filename becomes a technical debt trap.

Ultimately, if you must check the file name without introducing GET variables, basename($_SERVER['SCRIPT_NAME']) provides the most direct solution for standard PHP deployments. It requires no additional configuration and works out of the box for simple setups. Just remember that in larger applications or when using virtual hosting, verifying that the server-reported path matches your expectations is a necessary step to avoid subtle bugs in conditional rendering.

Our network