How can I echo current url and title for that page
7 posts by 4 authors in: Forums > CMS Builder
Last Post: November 14, 2011 (RSS)
By (Deleted User) - February 7, 2011
I am adding some Facebook and Twitter buttons to my sidebar. Now I know the JavaScript Like buttons pull the url and title automatically for the page when the button is selected.
I would like to also offer some static buttons for people who have JavaScript turned off. How can I add (echo) the current url and current title?
I would like to add the same code in the sidebar in multiple different pages and would like it to be automatic. Any ideas?
Thanks Jono
Re: [jonoc73] How can I echo current url and title for that page
By Jason - February 7, 2011
Here's a function that you can use to get the url of the current page you're on:
function curPageURL() {
$pageURL = 'http';
if (@$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
You can call it like this:
<?php echo curPageURL(); ?>
As for getting a page's title, there isn't a way in PHP to automatically detect this. The quickest way to get around this would be to set a $_REQUEST variable to store your page title. You can do this on every page and then pull it out on your sidebar script.
For example, on a page you can set the title like this:
<?php $_REQUEST['pageTitle'] = "My Page Title!";
On your side bar page you can access the variable like this:
<?php echo @$_REQUEST['pageTitle']; ?>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] How can I echo current url and title for that page
By Toledoh - November 9, 2011
I'm hoping to use the code above to identify the section of the site the viewer is in, and add a class="current" to the navigation buttons.
The site stucture is currently laid out like
/index.php
/contact.php
/terms.php
/manly/welcome.php
/manly/info.php
/manly/contact.php
/our_beers/welcome.php
/our_beers/our_beers.php
/shop/index.php
/shop/checkout.php
etc.
I have an inlcude that I use fot the site navigation... ie
<ul id="navlist">
<li><a href="/~murraysb/index.php" class="home"></a></li>
<li><a href="/~murraysb/our_beers/our_beers.php" class="beers">our beers</a></li>
<li><a href="/~murraysb/brewery/brewery.php" class="brewery">the brewery</a></li>
<li><a href="/~murraysb/manly/manly.php" class="manly">murray's @ manly</a></li>
<li><a href="/~murraysb/port_stephens_winery.php" class="psw">port stephens winery</a></li>
<li><a href="/~murraysb/shop/index.php" class="shop">shop online</a></li>
</ul>
I was hoping to do something like;
<?php if curPageURL() LIKE '/manly/' >
<li><a href="/~murraysb/manly/manly.php" class="manly active">murray's @ manly</a></li>
<?php else ?>
<li><a href="/~murraysb/manly/manly.php" class="manly">murray's @ manly</a></li>
or is there a better way to do this?
Tim (toledoh.com.au)
Re: [Toledoh] How can I echo current url and title for that page
By Jason - November 10, 2011
Try this:
<?php $currentFile = strtolower(basename(curPageUrl())); ?>
<?php if ($currentFile == "manly.php"):?>
<li><a href="/~murraysb/manly/manly.php" class="manly active">murray's @ manly</a></li>
<?php else:?>
<li><a href="/~murraysb/manly/manly.php" class="manly">murray's @ manly</a></li>
<?php endif ?>
This will return the name of the current file (example: manly.php) to be used in your if statements. Note that $currentFile will be in all lower case.
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] How can I echo current url and title for that page
By Toledoh - November 10, 2011 - edited: November 10, 2011
I was kinda looking for a "like" or "contains" so I could match a pettern back to the folder the file sits in. Thhe reason for this is there are a bunch of files within the "manly" section of the site, then a bunch more under "beer" etc.
I could have a naming convention to the actual files, rather than the folders... manly_page.php, manly_location.php, manly_contact.php etc...
Does that make sence?
Tim (toledoh.com.au)
Re: [Toledoh] How can I echo current url and title for that page
By (Deleted User) - November 14, 2011
You could try replacing:
<?php if ($currentFile == "manly.php"):?>
with:
<?php $pattern = '/^manly/'; ?>
<?php if ( preg_match ($pattern, $currentFile) ) : ?>
preg_match will look for the pattern 'manly' in whatever string you provide it and, if it returns true, the script will continue.
For more specific usage examples of preg_match, check out http://php.net/manual/en/function.preg-match.php
Hope that helps,
Tom
Re: [Tom P] How can I echo current url and title for that page
By Toledoh - November 14, 2011
Tim (toledoh.com.au)