This is a very simple PHP snippet that will set $title, $meta_keywords and $meta_description for use in your page header. You'll need to create meta_tags.txt which will use this format:
page_name.html|page title|meta tags|meta description
page_name2.html|page title2|meta tags for page 2|meta description for page 2
To use the code either save it at the top of your header file or save it as get_meta.php and add this line of code to your header:
<?php include('get_meta.php'); ?>
You can reference the $title, $meta_keywords and $meta_description variables in your header like this:
<title><?php print $title; ?></title>
<meta name="keywords" content="<?php print $meta_keywords; ?>">
<?php
$database = 'meta_tags.txt';
$meta_db = fopen($database, 'r');
$page = $_SERVER['SCRIPT_NAME'];
$page = substr($page, 1);
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == $page)
{
$title = $data[1];
$meta_keywords = $data[2];
$meta_description = $data[3];
}
}
?>
hawkee,
I have the title part working on my site,I used [if($category) print $category; if($subcategory) print $subcategory; print $title;] so that it would diplay $title before anyone clicked on $category or $subcategory. The value of $title is Products, so it just prints after $category or $subcategory is clicked, so my title becomes Art($category)Products($title).
My problem is that each page is loaded inside of my header as page1.html. All listings on the page are from $category or $subcategory. I'm not sure how to go about getting the keywords part to work. I don't need keywords for every page, just $category or $subcategory.
Your welcome to look at my site to see what I'm talking about.
Thank You
My variables:
$page = $_GET['page'];
$search = $_GET['search'];
$search = str_replace("-", " ", $search);
$search = stripslashes($search);
$category = $_GET['category'];
$subcategory = $_GET['subcategory'];
variable I added:
$title = ' Products';
The change I made:
<?php if($category) print $category; if($subcategory) print $subcategory; print $title; ?>this is the area I want to change because they stay the same:
What I am wondering is if I can create a php page structured like this
if($category='Arts_and_Entertainment') then ($meta_keywords='') and ($meta_description='');
if($category='Betting_Systems') then ($meta_keywords='') and ($meta_description='');
if($category='Business/Investing') then ($meta_keywords='') and ($meta_description='');
with all of my $categories and $subcategories keywords and descriptions and then call this page from my header?
My title works using this:
<?php if($category) print $category; if($subcategory) print $subcategory; print $title; ?>$title = ' Products';
onload title = Products, if any catagory (say Art) is clicked title = Art Products, and the same with the subcatagories
Is there a way that I could use the out-put from that to load keywords and descriptions for each category/subcategory?
@jrharper I'm sorry, but I'm just not very clear about what you are doing. Are you using the code provided on this page or are you building something entirely different? If you need some general programming questions answered you might want to try searching StackOverflow. It's a very good Q&A development resources.
Problem Solved !!!
First I want to thank you for the code and letting me use this place as a backboard for my thoughts.
I wanted to use your code, but I only have page1.html that is displayed inside a header. What is displayed on page1.html is controlled by 2 variables that make up the menu, $category and $subcategory. To solve my problem I created a title.php file that is an if/elseif statement.
if($category == '' && $subcategory == '') {
print('Products');
}
elseif ($category == 'Arts-and-Entertainment') {
print('Arts and Entertainment');
}
elseif($category == 'Betting-Systems') {
print('Betting Systems');
}
elseif($category == 'Business-/-Investing') {
print('Business / Investing');
It has $subcategory in the same file lower down.(it's 746 lines long) I did this to format the output. then I put include('title.php') between the title tags.
Now I can copy the file twice and save them as description.php and keyword.php and all I have to do is change the print('') statements and then include them between the tags.
the point is that my site uses a single page name and #xyz to show different content (but really they are different pages), a lot of sites do this now in order to avoid reloading and displaying content faster. but the problem indeed is that this page has a unique meta data right now. these are really different pages, but they are all under a single php file. for example gmail does this too: https://mail.google.com/mail/u/0/?shva=1#inbox
@pleb You'll probably need to reload the page with the anchor appended as a query string, but I'm not sure what your ultimate goal is. Anchors usually represent a particular point on a page and not a full page in itself. They're for browsers and not for spiders, so meta tags that are dependent on anchors don't make sense.
@pleb You'll need to incorporate JavaScript to read the hash portion of the URL. That information is not sent to PHP. Here is some more information, http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url
Hi Hawkee,
Running in a problem as my page is unique but shows different content using the # tag: index.php#home , index.php#contact etc...
I got it working on index.php but can't figure out how to structure get_meta and the data file to make it look at #home instead of just index.php
Thanks for your help
hello Hawkee
Please can u explain me how the following code works..
<?php
$database = 'get_meta.php';
$meta_db = fopen($database, 'r');
$page = $_SERVER['SCRIPT_NAME'];
$page = substr($page, 1);
echo $page;
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == $page)
{
$title = $data[1];
$meta_keywords = $data[2];
$meta_description = $data[3];
}
}
?>
Hawkee, Can you help?
I created a file and named it get_meta.php this is the code on that file:
<?php
$database = 'get_meta.php';
$meta_db = fopen($database, 'r');
$page = $_SERVER['SCRIPT_NAME'];
$page = substr($page, 1);
echo $page;
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == $page)
{
$title = $data[1];
$meta_keywords = $data[2];
$meta_description = $data[3];
}
}
?>
?movie=211|Horror Movie: 100 Feet|100 Feet, ghost story horror movie, ghost scary movie|A ghost story
I'm calling the file with this code on the header <?php include('get_meta.php'); ?> and have added this code also:
<?php print $title; ?>I have a MySQL movie database in my server. In the database the movie id is 211, the url for the same movie is http://www.besthorrormovielist.com/movieinfo.php?movie=211.
Do you see any typos and in get_meta.php is my format correct? I have been trying to fix this on my site for months and cannot get it to work.
Hey Guys, any thoughts on grabbing the URL (for instance, my users have their own urls (mysite.com)/(username)
I want to be able to rip username from URI and insert into Title so the page has unique content (currently all profiles have the same title/desc/kw) I was thinking i could regex the url, but would need some help incorporating it properly.
any ideas!
Thanks
I run MAMP, so my site sits in a subdirectory folder. Example: http://mysite.com/myfolder/test.php
test.php, meta_tags.txt, get_meta.php are on the same level.
In the meta_tags.txt file, I could have prepended 'myfolder' in front of all my page files to make this work. Example: myfolder/test.php
However that's counterproductive. Hawkee mentioned "strip the directory from $page"
However, I'm not fluent in PHP, so I didn't know how to do handle precisely. I searched the web for awhile to find a "solution."
Since the fix wasn't illustrated in this post, here is the solution that worked for me:
Comment out, but don't delete since you may need it later:
$page = substr($page, 1);
Add this snippet just below:
$page = ltrim(strrchr($_SERVER['PHP_SELF'], '/'), '/');
Basically, that seems to remove the subdirectory name from the path in order to make adding dynamic meta tags possible.
Hi
Great script I have the same problem as someone else above to get the script to work in sub folders. I have put the get_meta.php and meta_tags.txt in the root folder (public_html). Then I have sub folder with files and I used the echo $page varible and it says that the files are in "en/file1.php and "en/file2.php" and so on. Then I have put the patch "en/file1.php" in the meta_tags.txt but it want generate the tags.
Any suggestions?
correct it's for directory. when i include directory in meta_txt it works perfect. but I don't know how to stript the directory in get_meta.php.
what about meta information disapeearing http://www.ritzinfotek.in/ not the index.php
oh. everything works when it hosted in server. It won't work on my wamp local server..
see it here.. http://www.ritzinfotek.in
so I repeated the variable once more time but now the meta is taking but from the las line of database.. :
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == $page)
{
$data[1]=$title;
$data[2]=$meta_keywords;
$data[3]=$meta_description;
}
$title = $data[1];
$meta_keywords = $data[2];
$meta_description = $data[3];
}
I'm not too clear what you mean. Can you illustrate your point with some example URLs and the code changes you made?