Page 1 of 1

howto get current url to use in php script

Posted: Sat Jan 19, 2008 1:56 pm
by mrled
I have installed the runphp plugin to run a php script on a static content page. I used the runphp plugin to include a php page from includes directory:

{runphp} include_once 'includes/energie.php';{/runphp}

The script is running fine except for one issue:

I need the url of the current page for a form. I tried it this way:

$url = $_SERVER['PHP_SELF'];
" method="post" >

But this way i get redirected to the wrong page.

Any idea?

Re: howto get current url to use in php script

Posted: Sun Jan 27, 2008 9:11 pm
by Cheeky Monkey
Take a look at the information here: http://php.about.com/od/learnphp/qt/_SERVER_PHP.htm and see if using
$_SERVER['REQUEST_URI']  helps

Re: howto get current url to use in php script

Posted: Sun Jan 27, 2008 9:35 pm
by mrled
Thanks this was very helpfull!

Re: howto get current url to use in php script

Posted: Thu Nov 20, 2008 12:21 am
by juanparati
This method is better (Using the Joomla API):

Code: Select all

$myabsoluteurl=JURI::base();
But I don't know if is possible call to the JURI method directly from runphp plugin.

Please let us know about the results if somebody tries this trick. ???

Re: howto get current url to use in php script

Posted: Tue Oct 20, 2009 10:38 am
by circumpunct
juanparati wrote:This method is better (Using the Joomla API):

Code: Select all

$myabsoluteurl=JURI::base();
But I don't know if is possible call to the JURI method directly from runphp plugin.

Please let us know about the results if somebody tries this trick. ???
Yes, works for me. But JURI::base() is the base URL of course, not the page you are on. For that you need JURI::current()

Re: howto get current url to use in php script

Posted: Mon Mar 08, 2010 4:37 pm
by kflorida78
Don't make this too complicated...

Create a variable for the current page.

the baseurl() only finds the base url, you want the current() function.

Let's say you are on a page: http://www.mysite.com/widgits/red_widgits

You can get the Web site url with JURI::baseurl() like so:
Create a variable, then set it equal to the Joomla function:
$baseurl = JURI::base();

Now when you:
echo $baseurl;

It will display this:
http://www.mysite.com/

------------------------------------------------------
But you want the current absolute path url, correct? So do this instead:

Get the current absolute url with JURI::current() like so:
Create a variable, then set it equal to the Joomla function:
$currenturl = JURI::current();

Now when you:
echo $currenturl;

It will display this:
http://www.mysite.com/widgits/red_widgits

-------------------------------------------------------

Here is an example of how this can be used:

To call a stylesheet with the above created variables, replace the url info with the said variables.
Instead of:
<link rel="stylesheet" href="http://www.mysite.com/templates/theme/css/style.css" type="text/css" />

You can now do this:
<link rel="stylesheet" href="<?php echo $baseurl; ?>templates/theme/css/style.css" type="text/css" />

An example of the current() function submitting the current page to w3c validator:
instead of:
<a href="http://validator.w3.org/check?uri=http% ... %2F"><span class="w3cXHTML">&nbsp;&nbsp;&nbsp;→ Valid XHTML</span></a>

You can now do this:
<a href="http://validator.w3.org/check?uri=<?php echo $currenturl; ?>"><span class="w3cXHTML">&nbsp;&nbsp;&nbsp;→ Valid XHTML</span></a>

Hope that helps.

Re: howto get current url to use in php script

Posted: Wed Jun 23, 2010 11:19 am
by joras
Hi,

Just wondering if it is possible to get the url of the current page while not using sef urls.

For example:

I am working on this url -
abc.com/index.php?option=com_content&view=article&id=223&item_id=222&name1=value1&name2=value2

Is it possible to get "abc.com/index.php?option=com_content&view=article&id=223&item_id=222" from the above url?

Thanks,
joras

Re: howto get current url to use in php script

Posted: Wed Jun 23, 2010 11:55 am
by durexlw
joras wrote:Hi,

Just wondering if it is possible to get the url of the current page while not using sef urls.

For example:

I am working on this url -
abc.com/index.php?option=com_content&view=article&id=223&item_id=222&name1=value1&name2=value2

Is it possible to get "abc.com/index.php?option=com_content&view=article&id=223&item_id=222" from the above url?

Thanks,
joras
If you know the name of the parameters you want to get, you can build it yourself using getvar or getcommand

Re: howto get current url to use in php script

Posted: Wed Jun 23, 2010 11:58 am
by juanparati
Use the SERVER["REQUEST_URI"] variable.

See about it in:
http://php.net/manual/en/reserved.variables.server.php

Re: howto get current url to use in php script

Posted: Sat Jul 31, 2010 3:26 pm
by grayz
JURI::getInstance()->toString();
http://docs.joomla.org/JURI/getInstance

Re: howto get current url to use in php script

Posted: Sun Aug 01, 2010 12:04 pm
by juanparati
nice!

Re: howto get current url to use in php script

Posted: Fri Feb 25, 2011 3:41 pm
by paddyirishman
I'm looking to insert a Facebook Like button in Joomla. I want the button to like the CURRENT page, not the site. (i.e. I want 'YOUR_URL' in the code below to display the current url, not a predefined url.)

Code: Select all

<iframe src="http://www.facebook.com/plugins/like.php?href=YOUR_URL"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:80px"><iframe>
Any light on the situation???

Re: howto get current url to use in php script

Posted: Sun Feb 27, 2011 4:12 pm
by juanparati
Yes, use the Facebook Javascript API!

Re: howto get current url to use in php script

Posted: Fri May 20, 2011 5:25 am
by tez
grayz wrote:JURI::getInstance()->toString();
http://docs.joomla.org/JURI/getInstance
This is the correct answer, other methods fail when using index.php and extra URL parts.

Re: howto get current url to use in php script

Posted: Mon Jul 25, 2011 11:18 pm
by ncowen
I modified the code above to insert the rel canonical tag if the url has /articles in it.

<?php
$myabsoluteurl=JURI::current();
$canonical = strstr($myabsoluteurl, 'articles', true);
if($canonical == true) {
echo '<link rel="canonical" href="'. $myabsoluteurl .'"/>'; // echos canonical link
}
else { '<link href="'.$myabsoluteurl.'"/>';
}
?>

One issue I have is that it uses the full url. Is there a way to just use a relative url? Basically the code above includes sitename.com/articles/article-name, anyway to make it $myabsoluteurl only display /articles/article-name?

Re: howto get current url to use in php script

Posted: Tue Aug 09, 2011 7:29 pm
by drewgg
ncowen wrote:I modified the code above to insert the rel canonical tag if the url has /articles in it.

<?php
$myabsoluteurl=JURI::current();
$canonical = strstr($myabsoluteurl, 'articles', true);
if($canonical == true) {
echo '<link rel="canonical" href="'. $myabsoluteurl .'"/>'; // echos canonical link
}
else { '<link href="'.$myabsoluteurl.'"/>';
}
?>

One issue I have is that it uses the full url. Is there a way to just use a relative url? Basically the code above includes sitename.com/articles/article-name, anyway to make it $myabsoluteurl only display /articles/article-name?

JURI::current() is defined as:

Code: Select all

$uri     = & JURI::getInstance();
$current = $uri->toString( array('scheme', 'host', 'port', 'path'));
so try this (worked for for me):

Code: Select all

$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));
make note that this, as with JURI::current(), the query is not included. So you if you're routing is not complete and you land on a page with something like:

Code: Select all

http://www.sitename.com/articles/article-name?someQuery=notRouted
JURI::current() and &JURI::getInstance()->toString(array('path')); will both drop the ?someQuery=notRouted!

and if SEF urls are turned off then all you're going to get is http://www.sitename.com/index.php from JURI::current() and /index.php from &JURI::getInstance()->toString(array('path'));

If you DO want the query included then you'd want this ('fragment' might also be something to consider adding):

Code: Select all

&JURI::getInstance()->toString(array('path','query'));
More information on what you can pass into JURI::toString() and its usage: http://docs.joomla.org/JURI/toString

As grayz already mentioned, to get the complete url use &JURI::getInstance()->toString()

Re: howto get current url to use in php script

Posted: Thu Aug 25, 2011 6:32 am
by delhislicker

Re: howto get current url to use in php script

Posted: Thu Sep 22, 2011 10:45 pm
by Dasmonstrum2
durexlw wrote:
joras wrote:Hi,

Just wondering if it is possible to get the url of the current page while not using sef urls.

For example:

I am working on this url -
abc.com/index.php?option=com_content&view=article&id=223&item_id=222&name1=value1&name2=value2

Is it possible to get "abc.com/index.php?option=com_content&view=article&id=223&item_id=222" from the above url?

Thanks,
joras
If you know the name of the parameters you want to get, you can build it yourself using getvar or getcommand
Hi there, please did you find a solution for this?
Thanks

Re: howto get current url to use in php script

Posted: Thu Mar 15, 2012 6:31 am
by viktorz
it should work $uri = juri::current()

Re: howto get current url to use in php script

Posted: Mon Nov 26, 2012 12:57 pm
by LunchWillow
delhislicker wrote:well you can find it at http://www.7tech.co.in/php/get-url-of-c ... wordpress/
:)
That works for me del... I wanted to know for WP too, so double whammy there :laugh:

Re: howto get current url to use in php script

Posted: Tue Apr 23, 2013 3:14 pm
by yumpa
This problematic is described here with examples: http://www.escope.cz/en/blog/106-joomla ... h-examples

Re: howto get current url to use in php script

Posted: Mon Jan 06, 2014 3:23 pm
by Tubantix
To insert a facebook-like-button on all pages and for every url it's own button, I've done this is the template:
The head

Code: Select all

<head>
$currenturl = JURI::current();
</head>
the body

Code: Select all

<body>
<div class="fb-like" data-href="<?php echo $currenturl; ?>" data-layout="box_count" data-action="like" data-show-faces="true" data-share="false"></div>
</body>
Thanks to kflorida78

Re: howto get current url to use in php script

Posted: Mon Jan 06, 2014 5:45 pm
by Per Yngve Berg
You are looking up the api for the wrong version of Joomla.

http://api.joomla.org/framework-1.0/cla ... i.Uri.html