hi,

how do I get the url of the current page in a phptemplate based theme?

Comments

iokevins’s picture

I used this in my "Back to Top" link code:

<a href="/<?php print url($_GET['q'], NULL, NULL, TRUE); ?>#top">Back to top</a>

"Extraordinary claims require extraordinary evidence"
-- Carl Sagan

willis’s picture

drupal_get_path_alias(request_uri())

That's in 4.6.5. Not sure about other versions.

adityamenon’s picture

That code also works in templates on D6

usbank’s picture

$your_current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];

this code does not use drupal's own functions or variables, which will work well on all php projects. Replace "http://" with "https://" if your site is using ssl connection.

usbank’s picture

$your_current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];

this code does not use drupal's own functions or variables, which will work well on all php projects. Replace "http://" with "https://" if your site is using ssl connection.

usbank’s picture

$your_current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];

this code does not use drupal's own functions or variables, which will work well on all php projects. Replace "http://" with "https://" if your site is using ssl connection.

mlle.yotnottin’s picture

I've been using $this_page = $_SERVER['REQUEST_URI']; to get just the name of the currently displayed page without the domain path. This seems to always give me the name as drupal sees it *after* pathauto has done it's thing.

grunthus’s picture

The request_uri() function from bootstrap.inc looks like it automatically generates the required information, taking a different approach depending on what is available on the host platform:

http://api.drupal.org/api/function/request_uri/5

I've used it successfully.

Read 'Free Software - Free Society' by RM Stallman

DevElCuy’s picture

In addition request_uri should need to be used without the base_path part, it is useful specially for determining or setting current menu item (menu_set_active_item($path));

    $curi = urldecode(request_uri()); // decode URL removes annoying % and ##
    $curi = substr($curi, 1, strlen($curi));

--
(3 John 1:2) Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well.

--
develCuy
Hiring the next generation front-end devs, contact Dilygent

ncameron’s picture

Just a tip: I found myself using this with drupal_goto() (http://api.drupal.org/api/function/drupal_goto/5) to reload the current page on form submission. However, when using request_uri from a view or any URL with query it needs to be broken up into the path and the query parts for drupal_goto to compute it properly. The following if statement detects if the URL contains a query and if it does it breaks it up accordingly. Might save someone some time:

<?php
$this_page = request_uri();

//remove the first slash
$this_page = substr($this_page, 1, strlen($this_page));
	
// if the URL includes a query
if(strpos($this_page, '?')!==0){
  // break it up so it into path and query depending on the pos of the question mark
  $query = substr(strstr($this_page, '?'), 1, strlen($this_page));
  $this_page = substr($this_page, 0, strpos($this_page, '?'));
}
?>

--
Technology, Society and Economics in Latin America
http://altate.ch

ohnobinki’s picture

You don't need to give substr() a third argument if you want to just remove the first character of a string. Thus, your substr() line could look like:

$this_page = substr($this_page, 1);

Also, using !==0 will trigger that if statement whenever there is no '?' in the URI because strpos()'s return value of FALSE is not strictly equal to 0. You probably want

if (strpos($this_page, '?') !== FALSE) {
  list($this_page, $query) = explode('?', $this_page);
} else {
  $query = '';
}

Or, simpler, just the one-liner:

list($this_page, $query) = explode('?', substr(request_uri(), 1));

list() will assign NULL (which looks like '' when cast to a string) to $query if there is not a query section in $this_page. Avoiding the conditional (the if()) and its search through $this_page (which ends up being redundant with explode()'s search for '?') would seem to be more efficient ;-).

alvinj’s picture

Sorry, this is a bit of a newbie question, but I thought I should ask, is there a preferred way from a Drupal perspective to make sure this URL is "clean", and not some malicious user hack attempt? That is, if I get the URI like this:

$this_uri = request_uri();

is it a correct practice to process this URI through one of the methods on the Drupal secure coding page (http://drupal.org/writing-secure-code) before using it? The check_plain() method looks like it will encode the string, which is my major concern.

Thanks,
Al
http://devdaily.com

Barnettech’s picture

I'm using $curr_uri = check_plain(request_uri()); to make sure the url is clean.

hansrossel’s picture

ohnobinki’s picture

When playing with a URL in your code, you should always keep it in the `unclean' format until you render it for the user.

To ensure that it is clean at that point, you should really use something appropriate for the situation:

  • l() when writing a link
      return t('Click !here to read the latest updates.', array('!here' => l('here', $url)));
      
  • drupal_attributes() with url() when formatting an element in a custom way
      $attributes = array('alt' => t('[picture of a sphere]', 'src' => url($image_url))
      return '<img' . drupal_attribute($attributes) . '/>';
      
  • or one of the escaping t() substitutions (for example, in the following to display a URL entered by the user back to the user):
      drupal_set_message(t('The URL %url was successfully stored.', array('%url' => $new_url)));
      

    Here I use % which means that the value was obtained from the user and is being displayed to the user. In other cases where I need the URL included in t() but verbatim and only escaped, I would've used @.

ñull’s picture

This will return /es/etc for spanish pages. What if I don't want the language prefix? I found two api functions that will work fine: