Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

The easiest way to get the current URL path in PHP

Table of contents:

The easiest way to get the current URL path in PHP

Get the path from the current URL

You can get the current URL path in PHP using the $_SERVER superglobal. Here is a straightforward way to do it:

<?php
 
echo $_SERVER['REQUEST_URI'];

The REQUEST_URI key will give you the current URL path along with the query string (if any).

For example, if the current URL is https://www.example.com/foo?bar=baz, the above code will output /foo?bar=baz.

$_SERVER contains everything about the URL’s path

Now that you have the solution, let’s break it down:

  • $_SERVER is a superglobal exposed by PHP, which means it is available in all scopes throughout a script. It contains information about headers, paths, and script locations. And it’s not the only superglobal you can use.
  • ['REQUEST_URI'] is one of the elements of the $_SERVER superglobal that contains the URI (Uniform Resource Identifier). It includes both the path and the query string.

Use var_dump() on $_SERVER and see for yourself all the valuable information it contains.

Use cases for the URL’s path

  • Generating breadcrumbs: Breadcrumbs are a secondary navigation aid that helps users understand their location in an application. PHP lets you use the current URL path to generate dynamic breadcrumbs.
  • Highlighting the current page in a navigation menu: You can use the current URL path to compare with the menu items and highlight the active one dynamically.
  • Redirection: You may want to redirect users to the same page after they perform an action (e.g., submit a form).
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

7 Laravel RESTful APIs best practices for 2023 7 Laravel RESTful APIs best practices for 2023

Master the art of crafting RESTful APIs with Laravel thanks to these best practices.

Modified on Oct 10, 2023

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Modified on Nov 23, 2023

PHP
console.log() in PHP console.log() in PHP

Explore the world of PHP debugging with var_dump(), and Laravel's friendlier alternatives, dump() and dd(). Much charm, such useful!

Modified on Sep 6, 2023

PHP

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -