howto get current url to use in php script
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.
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.
-
- Joomla! Apprentice
- Posts: 5
- Joined: Sat Jan 19, 2008 1:44 pm
- Contact:
howto get current url to use in php script
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?
{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?
-
- Joomla! Apprentice
- Posts: 24
- Joined: Tue Jul 17, 2007 10:58 pm
Re: howto get current url to use in php script
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
$_SERVER['REQUEST_URI'] helps
-
- Joomla! Apprentice
- Posts: 5
- Joined: Sat Jan 19, 2008 1:44 pm
- Contact:
Re: howto get current url to use in php script
Thanks this was very helpfull!
- juanparati
- 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
This method is better (Using the Joomla API):
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.
Code: Select all
$myabsoluteurl=JURI::base();
Please let us know about the results if somebody tries this trick.
From the code to the reality!
- circumpunct
- 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
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()juanparati wrote:This method is better (Using the Joomla API):But I don't know if is possible call to the JURI method directly from runphp plugin.Code: Select all
$myabsoluteurl=JURI::base();
Please let us know about the results if somebody tries this trick.
Circumpunct
(Still a few of us Brianists around.)
(Still a few of us Brianists around.)
- kflorida78
- 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
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"> → Valid XHTML</span></a>
You can now do this:
<a href="http://validator.w3.org/check?uri=<?php echo $currenturl; ?>"><span class="w3cXHTML"> → Valid XHTML</span></a>
Hope that helps.
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"> → Valid XHTML</span></a>
You can now do this:
<a href="http://validator.w3.org/check?uri=<?php echo $currenturl; ?>"><span class="w3cXHTML"> → Valid XHTML</span></a>
Hope that helps.
-
- Joomla! Fledgling
- Posts: 4
- Joined: Fri Apr 24, 2009 2:57 am
Re: howto get current url to use in php script
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
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
-
- Joomla! Enthusiast
- Posts: 125
- Joined: Mon May 17, 2010 7:41 pm
Re: howto get current url to use in php script
If you know the name of the parameters you want to get, you can build it yourself using getvar or getcommandjoras 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
- juanparati
- 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
Use the SERVER["REQUEST_URI"] variable.
See about it in:
http://php.net/manual/en/reserved.variables.server.php
See about it in:
http://php.net/manual/en/reserved.variables.server.php
From the code to the reality!
-
- Joomla! Enthusiast
- Posts: 128
- Joined: Tue Dec 12, 2006 8:37 pm
Re: howto get current url to use in php script
JURI::getInstance()->toString();
http://docs.joomla.org/JURI/getInstance
http://docs.joomla.org/JURI/getInstance
Developing: boragroconsult.com
- juanparati
- Joomla! Enthusiast
- Posts: 123
- Joined: Thu Nov 08, 2007 6:00 pm
- Location: Aarhus (Denmark)
-
- Joomla! Apprentice
- Posts: 18
- Joined: Wed Jul 23, 2008 5:28 pm
Re: howto get current url to use in php script
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.)
Any light on the situation???
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>
- juanparati
- 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
Yes, use the Facebook Javascript API!
From the code to the reality!
-
- Joomla! Enthusiast
- Posts: 248
- Joined: Tue Nov 14, 2006 3:29 am
- Contact:
Re: howto get current url to use in php script
This is the correct answer, other methods fail when using index.php and extra URL parts.grayz wrote:JURI::getInstance()->toString();
http://docs.joomla.org/JURI/getInstance
-
- Joomla! Apprentice
- Posts: 23
- Joined: Tue Nov 02, 2010 3:43 pm
Re: howto get current url to use in php script
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?
<?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?
-
- Joomla! Enthusiast
- Posts: 161
- Joined: Thu Oct 30, 2008 7:27 pm
Re: howto get current url to use in php script
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'));
Code: Select all
$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));
Code: Select all
http://www.sitename.com/articles/article-name?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'));
As grayz already mentioned, to get the complete url use &JURI::getInstance()->toString()
-
- Joomla! Fledgling
- Posts: 1
- Joined: Thu Aug 25, 2011 6:29 am
Re: howto get current url to use in php script
well you can find it at http://www.7tech.co.in/php/get-url-of-c ... wordpress/
-
- Joomla! Apprentice
- Posts: 32
- Joined: Wed May 11, 2011 6:04 pm
Re: howto get current url to use in php script
Hi there, please did you find a solution for this?durexlw wrote:If you know the name of the parameters you want to get, you can build it yourself using getvar or getcommandjoras 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
Thanks
-
- Joomla! Fledgling
- Posts: 1
- Joined: Thu Mar 15, 2012 6:15 am
- LunchWillow
- Joomla! Apprentice
- Posts: 7
- Joined: Wed Nov 21, 2012 8:11 pm
Re: howto get current url to use in php script
That works for me del... I wanted to know for WP too, so double whammy theredelhislicker wrote:well you can find it at http://www.7tech.co.in/php/get-url-of-c ... wordpress/
A legend in my own lunchtime at http://www.indexicon.com
-
- Joomla! Apprentice
- Posts: 38
- Joined: Thu Mar 15, 2007 2:07 pm
Re: howto get current url to use in php script
This problematic is described here with examples: http://www.escope.cz/en/blog/106-joomla ... h-examples
-
- Joomla! Apprentice
- Posts: 18
- Joined: Thu Jan 15, 2009 6:11 pm
- Location: Brussel
Re: howto get current url to use in php script
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
the body
Thanks to kflorida78
The head
Code: Select all
<head>
$currenturl = JURI::current();
</head>
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>
- Per Yngve Berg
- Joomla! Master
- Posts: 30934
- Joined: Mon Oct 27, 2008 9:27 pm
- Location: Romerike, Norway
Re: howto get current url to use in php script
You are looking up the api for the wrong version of Joomla.
http://api.joomla.org/framework-1.0/cla ... i.Uri.html
http://api.joomla.org/framework-1.0/cla ... i.Uri.html