Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
How can I echo current url and title for that page

 

 


jonoc73
User

Feb 7, 2011, 11:29 AM

Post #1 of 7 (802 views)
Shortcut
How can I echo current url and title for that page Can't Post

Hello,

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


Jason
Staff / Moderator


Feb 7, 2011, 11:47 AM

Post #2 of 7 (801 views)
Shortcut
Re: [jonoc73] How can I echo current url and title for that page [In reply to] Can't Post

Hi Jono,

Here's a function that you can use to get the url of the current page you're on:


Code
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:


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

Code
 <?php $_REQUEST['pageTitle'] = "My Page Title!";


On your side bar page you can access the variable like this:


Code
 <?php echo @$_REQUEST['pageTitle']; ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Toledoh
Enthusiast

Nov 9, 2011, 6:20 PM

Post #3 of 7 (659 views)
Shortcut
Re: [Jason] How can I echo current url and title for that page [In reply to] Can't Post

Hi guys.

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



Code
<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;


Code
<?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?
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Jason
Staff / Moderator


Nov 10, 2011, 11:20 AM

Post #4 of 7 (615 views)
Shortcut
Re: [Toledoh] How can I echo current url and title for that page [In reply to] Can't Post

Hi Tim,

Try this:


Code
<?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 - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Toledoh
Enthusiast

Nov 10, 2011, 11:53 AM

Post #5 of 7 (613 views)
Shortcut
Re: [Jason] How can I echo current url and title for that page [In reply to] Can't Post

Thanks Jason,

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

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au

(This post was edited by Toledoh on Nov 10, 2011, 11:53 AM)


Tom P
User


Nov 14, 2011, 2:22 PM

Post #6 of 7 (590 views)
Shortcut
Re: [Toledoh] How can I echo current url and title for that page [In reply to] Can't Post

Hello Tim,

You could try replacing:


Code
<?php if ($currentFile == "manly.php"):?>


with:


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


Toledoh
Enthusiast

Nov 14, 2011, 2:40 PM

Post #7 of 7 (585 views)
Shortcut
Re: [Tom P] How can I echo current url and title for that page [In reply to] Can't Post

Worked straight out-of-the-box... thatnks heaps Tom!
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au