howto get current url to use in php script

Discuss the development and implementation of Joomla! bots/Plugins here.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
mrled
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Jan 19, 2008 1:44 pm
Contact:

howto get current url to use in php script

Post by mrled » Sat Jan 19, 2008 1:56 pm

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?

Cheeky Monkey
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Tue Jul 17, 2007 10:58 pm

Re: howto get current url to use in php script

Post by Cheeky Monkey » Sun Jan 27, 2008 9:11 pm

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

mrled
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Jan 19, 2008 1:44 pm
Contact:

Re: howto get current url to use in php script

Post by mrled » Sun Jan 27, 2008 9:35 pm

Thanks this was very helpfull!

User avatar
juanparati
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 123
Joined: Thu Nov 08, 2007 6:00 pm
Location: Aarhus (Denmark)

Re: howto get current url to use in php script

Post by juanparati » Thu Nov 20, 2008 12:21 am

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. ???
From the code to the reality!

User avatar
circumpunct
Joomla! Intern
Joomla! Intern
Posts: 97
Joined: Mon Aug 27, 2007 7:30 am
Location: 54°58'47.86"N 2°15'49.28"W

Re: howto get current url to use in php script

Post by circumpunct » Tue Oct 20, 2009 10:38 am

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()
Circumpunct
(Still a few of us Brianists around.)

User avatar
kflorida78
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Wed Dec 19, 2007 3:36 pm
Location: Houston, TX
Contact:

Re: howto get current url to use in php script

Post by kflorida78 » Mon Mar 08, 2010 4:37 pm

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.
Kevin Florida
Project Manager
Florida Host Web, Inc.
www.floridahostweb.com

joras
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Apr 24, 2009 2:57 am

Re: howto get current url to use in php script

Post by joras » Wed Jun 23, 2010 11:19 am

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

durexlw
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 125
Joined: Mon May 17, 2010 7:41 pm

Re: howto get current url to use in php script

Post by durexlw » Wed Jun 23, 2010 11:55 am

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

User avatar
juanparati
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 123
Joined: Thu Nov 08, 2007 6:00 pm
Location: Aarhus (Denmark)

Re: howto get current url to use in php script

Post by juanparati » Wed Jun 23, 2010 11:58 am

Use the SERVER["REQUEST_URI"] variable.

See about it in:
http://php.net/manual/en/reserved.variables.server.php
From the code to the reality!

grayz
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 128
Joined: Tue Dec 12, 2006 8:37 pm

Re: howto get current url to use in php script

Post by grayz » Sat Jul 31, 2010 3:26 pm

JURI::getInstance()->toString();
http://docs.joomla.org/JURI/getInstance
Developing: boragroconsult.com

User avatar
juanparati
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 123
Joined: Thu Nov 08, 2007 6:00 pm
Location: Aarhus (Denmark)

Re: howto get current url to use in php script

Post by juanparati » Sun Aug 01, 2010 12:04 pm

nice!
From the code to the reality!

paddyirishman
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Wed Jul 23, 2008 5:28 pm

Re: howto get current url to use in php script

Post by paddyirishman » Fri Feb 25, 2011 3:41 pm

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???

User avatar
juanparati
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 123
Joined: Thu Nov 08, 2007 6:00 pm
Location: Aarhus (Denmark)

Re: howto get current url to use in php script

Post by juanparati » Sun Feb 27, 2011 4:12 pm

Yes, use the Facebook Javascript API!
From the code to the reality!

tez
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 248
Joined: Tue Nov 14, 2006 3:29 am
Contact:

Re: howto get current url to use in php script

Post by tez » Fri May 20, 2011 5:25 am

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.

ncowen
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Tue Nov 02, 2010 3:43 pm

Re: howto get current url to use in php script

Post by ncowen » Mon Jul 25, 2011 11:18 pm

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?

drewgg
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 161
Joined: Thu Oct 30, 2008 7:27 pm

Re: howto get current url to use in php script

Post by drewgg » Tue Aug 09, 2011 7:29 pm

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()

delhislicker
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Aug 25, 2011 6:29 am

Re: howto get current url to use in php script

Post by delhislicker » Thu Aug 25, 2011 6:32 am


Dasmonstrum2
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed May 11, 2011 6:04 pm

Re: howto get current url to use in php script

Post by Dasmonstrum2 » Thu Sep 22, 2011 10:45 pm

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

viktorz
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Mar 15, 2012 6:15 am

Re: howto get current url to use in php script

Post by viktorz » Thu Mar 15, 2012 6:31 am

it should work $uri = juri::current()

User avatar
LunchWillow
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 21, 2012 8:11 pm

Re: howto get current url to use in php script

Post by LunchWillow » Mon Nov 26, 2012 12:57 pm

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:
A legend in my own lunchtime at http://www.indexicon.com

yumpa
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Thu Mar 15, 2007 2:07 pm

Re: howto get current url to use in php script

Post by yumpa » Tue Apr 23, 2013 3:14 pm

This problematic is described here with examples: http://www.escope.cz/en/blog/106-joomla ... h-examples

Tubantix
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Thu Jan 15, 2009 6:11 pm
Location: Brussel

Re: howto get current url to use in php script

Post by Tubantix » Mon Jan 06, 2014 3:23 pm

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

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30888
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: howto get current url to use in php script

Post by Per Yngve Berg » Mon Jan 06, 2014 5:45 pm

You are looking up the api for the wrong version of Joomla.

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


Locked

Return to “Plugins/Mambots”