Page 1 of 1

write current url to page

Posted: Sat Oct 30, 2004 4:59 am
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.

Posted: Sat Oct 30, 2004 9:51 am
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.

Posted: Sat Oct 30, 2004 12:51 pm
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

Posted: Sat Oct 30, 2004 9:36 pm
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.

Posted: Sat Oct 30, 2004 10:21 pm
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.

SOLVED

Posted: Mon Nov 01, 2004 3:38 am
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.