write current url to page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lawsim
Forum Newbie
Posts: 3
Joined: Sat Oct 30, 2004 4:44 am
Location: South Australia

write current url to page

Post by lawsim »

I would like to use PHP and *.inc files to add head, navigation and footer includes to help in site maintenance, (this part I have done). I would like to include the current page name as part of the 'head.inc' file, that is, when a user clicks on a link (or navigation button) to go to a page, php checks the url for that page ie "fredsite\home.php", and writes the name "Home Page" in the appropiate spot for that page. Can anybody point me in the right direction?
Thanks in advance.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Think about it logically. To begin with the page doesnt know what it is loading. So you check the url. Now you know what page to load - but not what its called. So to find out what its called you must have somewhere a list to tell you what its called. What can hold a list? An array, a database.

So having found the page you are loading check it in an array or with the database to get the name.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Use javasript to recieve current document location (you need to get that data to php-variable and print it, ot use javascripts document.write)

Code: Select all

<?php
echo "<SCRIPT TYPE="text/JavaSCRIPT"> \r\n";
echo "<!-- begin \r\n";
echo "alert(document.location);";
echo "// end -->\r\n";
echo "</SCRIPT>\r\n";
?>
You can receive the same info by global variables, but it's more complex
Use var_export($_SERVER); to examine this method
lawsim
Forum Newbie
Posts: 3
Joined: Sat Oct 30, 2004 4:44 am
Location: South Australia

Post by lawsim »

Thanks for the input, but I want to use PHP and not a combo. I just need the program to check the page name, and use "if" statements to go thru a list of pages, and then put match in variable, and then put that variable in current page to display that pages name, i.e.."Home Page", "Contact Us", or whatever it is. I do not want to change the underlying structure of the page, i.e.. the tables and columns, that is why I need PHP to insert the name. Can it be done?, any code snippets would be helpfull.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

switch($page_url){
   case 'home.php':
      $page_title = "Home";
      break;
   case 'contact.php';
      $page_title = "Contact Us";
      break;
}
Etc. Or you can store the values in an array or the database and get them that way instead of having 200 cases in the switch when your site grows.
lawsim
Forum Newbie
Posts: 3
Joined: Sat Oct 30, 2004 4:44 am
Location: South Australia

SOLVED

Post by lawsim »

Thanks for all the input, I have tested the following code, and it works ok.

<?php
$page_url = ($_SERVER['PHP_SELF']);
switch($page_url) {
case '/mysite/home.php':
$page_title = "Home page";
print($page_title);
break;

}
?>

once again, thanks, Barry.
Post Reply