2

I'm trying to output the current page URL on a Drupal site (Drupal 7), I've tried the following...

<?php print current_path(); ?>

Which outputs the page name but not the URL, any ideas on how to output the full URL too?

steffcarrington
  • 404
  • 3
  • 13

5 Answers5

2

In straight PHP you can use this:

$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];

I don't see why that wouldn't work. That will give you the path and the URL.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
2

You can use

<?php 
    global $base_url;
    print $base_url . '/' . current_path(); 
?>
1

If you want to keep things done "the drupal way", you can use current_path() or request_path().

https://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7 https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7

You can also use $base_url instead of relying on $_SERVER.

The solution durbpoisn gave will give you the URL in the browser. If that is an alias, you can use drupal_get_normal_path() to get the internal path

https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7

Tim
  • 485
  • 2
  • 9
1

in drupal9

$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
Matoeil
  • 6,851
  • 11
  • 54
  • 77
0

On D7 if you want to get the current path you can use:

$path = drupal_get_path_alias();

So if your current URL is:

www.test.com/hello/world

You will get:

'hello/world'

Davide Casiraghi
  • 15,591
  • 9
  • 34
  • 56