Reputation: 709
I'm a bit stuck on this code. It worked "before" on a different site but now for some reason it does not work for this new one I am working on. I have a separate PHP file that I store meta title, meta description, meta keywords, h1, h2 tags in as an array. The website is supposed to fetch that information divy up into variables and then it is placed in the correct positions within the code. Here is the code as follows:
SEO.PHP File in the root folder of the website
<?php
$meta['INDEX']['title'] = "Title";
$meta['INDEX']['keywords'] = "keywords";
$meta['INDEX']['description'] = "description";
$meta['INDEX']['H1'] = "h1";
$meta['INDEX']['H2'] = "h2";
$meta['ABOUT']['title'] = "About Company";
$meta['ABOUT']['keywords'] = "kwd1, kwd2, kwd3";
$meta['ABOUT']['description'] = "About company description";
$meta['ABOUT']['H1'] = "h1 title";
$meta['SERVICES']['title'] = "About Company";
$meta['SERVICES']['keywords'] = "kwd1, kwd2, kwd3";
$meta['SERVICES']['description'] = "About company description";
$meta['SERVICES']['H1'] = "h1 title";
$meta['BLOG']['title'] = "About Company";
$meta['BLOG']['keywords'] = "kwd1, kwd2, kwd3";
$meta['BLOG']['description'] = "About company description";
$meta['BLOG']['H1'] = "h1 title";
?>
Example of code in INDEX.PHP
<?php
include 'seo.php';
$page_index = array_keys($meta);
foreach($page_index as $page)
{
if ( strpos( strtoupper($_SERVER['REQUEST_URI']), $page ) !== false)
{
$title = $meta[$page]['title'];
$keywords = $meta[$page]['keywords'];
$description = $meta[$page]['description'];
$h1 = $meta[$page]['H1'];
$h2 = $meta[$page]['H2'];
break;
}
} ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
Here is what is printed out from the various variables for troubleshooting.
print_r ($page_index);
Array ( [0] => INDEX [1] => ABOUT [2] => SERVICES [3] => BLOG )
print_r($meta);
Array ( [INDEX] => Array ( [title] => Plumbers in Birmingham, AL | Birmingham Plumbers
[keywords] => Plumbers in Birmingham AL, Birmingham Plumbers, Plumbers Birmingham AL,
Plumber Birmingham AL, Plumbing Birmingham Al, Hoover Plumbing, Plumbers in Hoover Al
[description] => Plumbers in Birmingham, AL - Servicing Mountain Brook, Vestavia
Hills, Hoover, Pelham, Alabaster, Helena, Homewood and more locations. [H1] => The
Best Plumbers in Birmingham, AL [H2] => 24 Hour Emergency Plumbing Service ) [ABOUT]
=> Array ( [title] => About Company [keywords] => kwd1, kwd2, kwd3 [description] =>
About company description [H1] => h1 title ) [SERVICES] => Array ( [title] => About
Company [keywords] => kwd1, kwd2, kwd3 [description] => About company description [H1]
=> h1 title ) [BLOG] => Array ( [title] => About Company [keywords] => kwd1, kwd2,
kwd3 [description] => About company description [H1] => h1 title ) )
These print nothing at all...
print_r ($title);
print_r ($keywords);
print_r ($description);
print_r ($h1);
print_r ($h2);
And finally....
print_r ($page);
displays BLOG
So, I can see that the code is reading the php file and can read the array as can be seen with meta and page index.
But it is breaking down on the variables and also the page. Since this was tested on the index page and not the blog page - yet it returns blog.
Any ideas?
Upvotes: 2
Views: 128
Reputation: 194
Check if your pathes are correct, check output of print(__FILE__);
print($_SERVER['DOCUMENT_ROOT']);
from index.php.
Try building your path with __FILE__
and pathinfo. Maybe realpath is required to ensure path structure.
Also it is a good idea to not simply set $meta in a file, but instead return a generated array of the content like: array( 'title' => 'Title text', 'kexwords' => 'kexwords text', 'description' => 'description text', // ... ), 'ABOUT' => array( 'title' => 'Title text', 'kexwords' => 'kexwords text', 'description' => 'description text', // ... ), 'SERVICES' => array( 'title' => 'Title text', 'kexwords' => 'kexwords text', 'description' => 'description text', // ... ), // ... );
and in other files
<?php
//file index.php
$meta = include( $_SERVER['DOCUMENT_ROOT'] . '/seo.php' );
both ways should work tough.
Upvotes: 0
Reputation: 78994
Lots of ways to do this (try $_SERVER['SCRIPT_NAME']
or $_SERVER['PHP_SELF']
with basename()
:
if ( strpos( basename(strtoupper($_SERVER['SCRIPT_NAME'])), $page ) !== false)
Or maybe:
if ( strpos( basename(strtoupper($_SERVER['PHP_SELF'])), $page ) !== false)
Or to provide a default value you can try (might tweak what $_SERVER
var you use here as well):
$page_index = array_keys($meta);
$vars = $meta['INDEX'];
foreach($page_index as $page){
if ( strpos( strtoupper($_SERVER['REQUEST_URI']), $page ) !== false) {
$vars = $meta[$page];
break;
}
}
$title = $vars['title'];
$keywords = $vars['keywords'];
$description = $vars['description'];
$h1 = $vars['H1'];
$h2 = $vars['H2'];
//could possibly use extract($vars); instead
Upvotes: 3