scm22ri Posted November 13, 2012 Share Posted November 13, 2012 (edited) Hi Everyone, How would I dynamically change meta tag information on a per page basis? If a visit is on my "about" or "inventory" page. In the title and meta tag I would want that user to see "about us" or "inventory". I've written some below sample code but it's not working. Not sure what I'm doing. For example, this page should be echoing out "Title Test Page" but it's not. What am I doing wrong here? Thanks! http://whatsmyowncar...3/titletest.php <?php $title; switch($_SERVER['PHP_SELF']) { case "/index.php": $title = 'Home'; break; case "/titletest.php": $title = 'Title Test Page'; break; } print '<title>'.$title.'</title>'; ?> Edited November 13, 2012 by scm22ri Quote Link to comment https://forums.phpfreaks.com/topic/270645-how-would-i-dynamically-change-meta-tag-information/ Share on other sites More sharing options...
Adam Posted November 13, 2012 Share Posted November 13, 2012 I'm guessing you have a shared "header.php" type file you include across each page? Just define a default config array within your header file, and allow each page to override it: header.php $defaultConfig = array( 'title' => 'Your Site', 'description' => 'Your site description', 'keywords' => 'your, site, keywords' ); $config = array_merge($defaultConfig, isset($config) ? $config : array()); about.php $config = array( 'title' => 'About page' // Default description and keywords will be used as we omitted them here ); include 'header.php'; Keeps it nice and simple, and avoids a huge switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/270645-how-would-i-dynamically-change-meta-tag-information/#findComment-1392153 Share on other sites More sharing options...
scm22ri Posted November 14, 2012 Author Share Posted November 14, 2012 Thanks Adam, I went about this in a slightly different manner. What do you think about my answer? I did in fact use a switch statement. I requested the url as a unique identifier of each page. According to the url it's going to return a specific statement. <?php $title; $url = $_SERVER['REQUEST_URI']; switch($url){ case "/class-work/sign3/valchecklogin.php"; $title = 'Home'; break; case "/class-work/sign3/heythere.php"; $title = 'Profile Page'; break; case "/class-work/sign3/display-inventory.php"; $title = 'Inventory Page'; break; case "/class-work/sign3/mygoal.php"; $title = 'My Goal'; break; case "/class-work/sign3/add-car.php"; $title = 'Add Car'; break; case "/class-work/sign3/add-dealership.php"; $title = 'Add Cardealership'; break; case "/class-work/sign3/edit-car.php"; $title = 'Edit Cars'; break; case "/class-work/sign3/cal.php"; $title = 'Calculator'; break; // the results page poses a issue. Not quite sure how to fix this? case "/class-work/sign3/cal-results.php?"; $title = 'Calculator Results'; break; case "/class-work/sign3/forgot2.php"; $title = 'Forgot Password'; break; case "/class-work/sign3/change-password.php"; $title = 'Change Password'; break; case "/class-work/sign3/register.php"; $title = 'Register'; break; case "/class-work/sign3/contact.php"; $title = 'Contact Us'; break; case "/class-work/sign3/logout.php"; $title = 'Logout'; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270645-how-would-i-dynamically-change-meta-tag-information/#findComment-1392320 Share on other sites More sharing options...
Adam Posted November 15, 2012 Share Posted November 15, 2012 the results page poses a issue. Not quite sure how to fix this? Well to be honest, those are the kind of problems you'll face splitting out the logic of what title to display, from the logic that actually determines what title should be displayed. If that makes sense? Quote Link to comment https://forums.phpfreaks.com/topic/270645-how-would-i-dynamically-change-meta-tag-information/#findComment-1392555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.