Hey guys! First post! wooooot! Since it's my first, I figured I'd post somethin' GOOD. When I make a website, I HATE managing tons and tons of <head></head> tags. It’s a complete waste of time. I rather prefer to use php and include a header file. ie: include(’header.php’); The only problem I ran into with that approach was that I couldn’t have different page titles keyed into each pages individual content. Since the <title></title> tag is expressed in the header.php file, it is the same for all pages on the site that include it. So, I came up with a solution. It’s not an elegant one, I’m sure. In fact, Im certain there has to be an easier way then this, but it works! This example defines a variable $loc with it’s the url of the page your are currently loading which is then called by the file_get_contents command. That data is then scraped and everything is discarded expect what is wrapped between the <strong></strong> tags, which I use for the title of each of my articles. That results is then cleaned up and passed into the HTML <title></title> code via the variable $title I imagine this method is bound to be pretty much impossible for large sites to use, as it basically loads the page twice…. HEADER.PHP <?php $currentfile = $_SERVER[’DOCUMENT_ROOT’] . $_SERVER[â€PHP_SELFâ€]; $fd = fopen($currentfile, “râ€); $contents = fread($fd,filesize ($currentfile)); fclose ($fd);; $regex = ‘@<strong>(.+?)</strong>@’; preg_match($regex,$contents,$match); $title_raw = $match[0]; $title = trim($title_raw, “<strong></strong>â€); echo ‘ <html> <head> <title> ‘; echo $title; echo ‘ - Static part of site title</title> </head> <body>’; ?>
That's a big load to do. You can create a function that takes a page identifier to create portions of the page. include with file reading or regex (what you suggest) might take longer than you expect (not so long but when the app will use many includes etc...). Here's a sample function do_title($page=FALSE) { if($page=='monkeyPage') { $title = 'Funky monkey business'; } else { $title = 'default title'; } return '<title>'.$title.'</title>'; } PHP: and then in the page you simply call the function which cand have a title or not. <?=do_title()?> in the above example it will output the default title and using the one below it will output the title for the indicated page <?=do_title('monkeyPage')?>
Your function will not generate relevant and human readable titles!!! if you are using this function , can you give us examples of titles PLZ?
Any reason you can't specify the page title in the original source file before you include the header? I.e. content pages: # Output header $pageTitle = 'This page name'; include 'header.php'; PHP: header.php ... <title><?php echo empty($pageTitle) ? 'default page title' : $pageTitle; ?></title> ... PHP:
my own website is the best example. You have to work for this shit not expect for some magic php function to create good seo titles on the fly while you sit back and drink a beer in Jamaica. I have manually created titles and metas and rules for almost all my pages (and I do have some believe me).
If your website is database driven (when all the pages are in a database) you could consider to extend your page-table in your database with aditional collums for the metatags and title...
Wow. I love this forum. Im already learning stuff! Yeah. My script is perhaps not the most efficient. Im definately no PHP whiz. I'll try it your guys' way and see what the results are! Ill report back! THanks for the insight!
im with Rodney on this one, the best way is to define the variable for your page title before your header include. To take that a step further, I wrote a function that prints my meta tags, it take 4 arguments: Page Title, Page Keywords, Page Description and Path to CSS So when I make a page I simply call the header.php file and the variables like this: $pageTitle = "blah"; $pageKW = "blah blah"; $pageDesc = "blah bleh meh"; $cssPath = "../"; (Depends on the current dir) require_once("header.php"); The header.php has the printMeta function in it and takes the arguements I specified and echos out the all of the meta information, meta robots tags, css link, etc.
Yeah, but I used my method for redesigning and optimizing a website of over 700 pages. I didn't fancy defining that many variables. I realized that almost all the pages had headlines with strong tags so that was my method for quickly and easily optimizing the titles of the entire site without spending hours doing tedious work... But your guys' snippet provide me with some interesting perspective on how to arrange my headers for future web designs. THANKS!
If you keep doing regex over and over again, especially with a lot of pages... regex takes some time.