I need help with making custom <title>'s. Here is an example... If the url is on homepage being either http://website.com, http://www.website.com, http://website.com/index.php, or http://www.website.com/index.php echo BLAH BLAH else MOO MOO Thank you
Not sure why you need this , but here goes, easy as pie: switch ($_SERVER['HTTP_HOST']) { case 'website.com': $title = ($_SERVER['REQUEST_URI'] == '/index.php' ? 'URL is website.com/index.php' : 'URL is website.com'); break; case 'www.website.com': $title = ($_SERVER['REQUEST_URI'] == '/index.php' ? 'URL is www.website.com/index.php' : 'URL is www.website.com'); break; default: $title = 'Nothing else matched'; } echo '<title>' . $title . '</title>'; PHP:
Not quite working as planned When the url is www.website.com/folder/ it still displays the case 'www.website.com': stuff. Rather then the variable I assigned to 'default'
Well, the code was designed to only handle the four cases you specified in your original post ...but it's simple to change around: switch ($_SERVER['REQUEST_URI']) { case '/index.php': $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com/index.php' : 'URL is www.website.com/index.php'); break; case '/': $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com' : 'URL is www.website.com'); break; default: // anything else $title = 'Nothing else matched'; } echo '<title>' . $title . '</title>'; PHP: (This also has a built-in assumption/limitation, in that this only handles two host names, website.com & www.website.com. If you need more, you need to rewrite the ternary expressions into switch/case statements, or better yet, explain why you really need this so it can be rewritten explicitly for that purpose.)
Okay, I have a content system that I am trying to tweak. Basicly the title for all the pages was in the following format <title> <php websitename; ?> - <php pagename; ?> </title> See I dont want to have on everypage the name of the website name apear. So what I am trying to do is when i am on the 'index' file meaning the Home of the website I want it to show <php websitename; ?>. WHen it's on a page I want it to have <php pagename; ?> in the title
hmmm what cms? I think the issue here is your not being specific... you cited 4 exampes and got specific answers for those questions. Now you throw in the cms and well... that could get difficult. I will take a crack at it based on your post of the varible pagename in the title... if(isset($_GET['pagename']) && !empty($_GET['pagename'])) { #pass pagename to the title echo "<title>".$_GET['pagename']." </title>\n"; }else { #if no pagename echo "<title>Default </title>"; } PHP:
The cms isn't part of the problem. I am just explaining what it's for. Okay Willy got it right with the second code. However I have one more problem. switch ($_SERVER['REQUEST_URI']) { case '/index.php': $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com/index.php' : 'URL is www.website.com/index.php'); break; case '/': $title = ($_SERVER['HTTP_HOST'] == 'website.com' ? 'URL is website.com' : 'URL is www.website.com'); break; default: // anything else $title = 'Nothing else matched'; } echo '<title>' . $title . '</title>'; PHP: For this part default: // anything else $title = 'Nothing else matched'; I place websitename; in the $title. websitename; is the function which gets the title of the page I assign. However when I do that it just makes my title "websitename;" How would I make it act as a php function like it's suppose to.
If you have a CMS does it have a database? If so does the DB have a field where you can store title? I use my own cms which has title key and desc fields and simply have php call at the beginning of the page (before html) and a default title if fails. If not you may want to get familiar with sql as it will become messy if you try to put in fixed vars in the headers. Expat
This wont work as the function echos in that making the code look like <head> TITLE HERE<title></title>
All right, now that it's clearer what you'd like to do, how about simply this $titles = array( '/index.php' => 'Home page title', '/' => 'Home page title', '/someotherpage.html' => 'Explicit title for some other page', ); echo '<title>'; if (array_key_exists($_SERVER['REQUEST_URI'], $titles)) { // for any page we've explicitly specified a title for, use that: echo $titles[$_SERVER['REQUEST_URI']]; } else { // for any other page, call the CMS function which outputs the title websitename(); } echo '</title>'; PHP:
My pleasure (Exam's ob_start tip, above, is also good to remember for any similar situation where a function insists on directly outputting something instead of "nicely" returning it.)