Is there a way to add page titles and meta tags dynamically using PHP (and MySQL)?

Hi,  I'm creating a PHP and MySQL web application for a client. I'm a fairly new web developer.
Is there a way to add page titles and meta tags dynamically? It's a small application. I'm not using a Framework.
I saw a couple of possible solutions on google, but am not sure what the easiest and most efficient way of doing it would be.

Possible Solution #1, add variables in my header.php page and then on each respective page for example about, contact etc... add those variables above the include statement.
Here is my header.php code:

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="<?php echo $meta_description; ?>">
	<meta name="author" content="Ryan Sacks">
	<link rel="icon" href="img/favicon.ico">
	
	<title><?php echo $page_title; ?></title>
	
	<!-- Bootstrap core CSS -->
	<link href="css/bootstrap.min.css" rel="stylesheet">
	
	<!-- Font Awesome Icons -->
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
	
	<!-- Custom CSS -->
	<link href="css/style.css?v=<?php echo time(); ?>" rel="stylesheet">
	
	<!-- Google Fonts -->
	<link href="https://fonts.googleapis.com/css?family=Oswald:300,400,500" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
	
	<!-- Animate CSS -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
  
  	<!-- Breadcrumbs CSS -->
	<link rel="stylesheet" href="css/breadcrumbs.css">
	
	<!-- HTML5 shiv and Respond.js IE8 support of HTML5 elements and media queries -->
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->
	
	<!-- ReCaptcha v2 -->
	<script src='https://www.google.com/recaptcha/api.js'></script>
</head>

Open in new window


and in my index.php:
$page_title = "Hockey Pool Fundraiser | A fun and simple way to save thousands of dollars for your team";

$meta_description = "A simple pool-style Hockey program";

include('includes/header.php');

Open in new window


Possible Solution #2, I saw something about creating a function and using a switch statement but I'm not exactly sure what that code would look like. I have about 20 pages in the application.
Can anyone help?
Ryan SacksGraphic and Web Designer / DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Tip: You can use WordPress + you'll solve your current requirement + many more.

Or, you can download WordPress + go through their code to see how they approach solving your requirement + just copy... er... I mean... use their code as an example to tool some similar code...
Ryan SacksGraphic and Web Designer / DeveloperAuthor Commented:
My client didn’t want to use WordPress for 2 reasons.
1) we would’ve needed to buy a plan for a Form Plugin and he didn’t want to pay $30 USD a month forever plus Add Ons.
2) He didn’t want to rely on a 3rd party Plugin and
3) I wasn’t comfortable using Hooks and Filters.

WordPress uses custom functions and other code only specific to WordPress. So its not the exact same code as building a php application Without using WordPress. Ps. I know WordPress is awesome and powers about 35% of all websites.

I think a php developer should be able to help me.
kenfcampCommented:
Well, when I need to do this I usually do something like (using your code as an example):

<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
         <? $qpgval="somepage1"; include ("dynmeta"); ?>

-- continued html --

Open in new window


dynmeta (quick example)

<?php if(isset($qpgval) && ){if ($qpgval == "somepage1"){?>
                <title>Somepage1 title</title>
                <meta charset="utf-8">
                <meta name="description" content="page description"/>
                <meta name="keywords" content="some keywords"/>

<?php }else if($qpgval == "somepage2"){?>
                <title>Somepage2 title</title>
                <meta charset="utf-8">
                <meta name="A different page description"/>
                <meta name="keywords" content="Different keywords"/>

<?php 
}
}
?>

Open in new window


Hope it helps
Ken
Scott FellDeveloper & EE ModeratorCommented:
For something very basic, you want to direct all traffic to an index page where the index page is your router.  From there you can gather variables and push that to a template page.

Assume you send an array called $data to your template page that look like below.

<?php
$metaDescription = data["metaDescription"];
$title = data["title"];
$body = data["body"];
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="description" content="<?php echo $metaDescription ?>"/>
  <title><?php echo $title ?></title>
</head>
<body>
<?php echo $body ?>
</body>
</html>

Open in new window


If you look at some of the "basic" php routers and start playing with them, you realize there are things missing.  To save yourself some time, give a framework a try. One place to start is https://www.slimframework.com/ and you will find it will be easier to use a template engine and there are examples using twig https://www.slimframework.com/docs/v3/features/templates.html

While it is nice to use a framework with a seemingly small footprint, you will find using a more robust framework like Cake or Laravel https://laravel.com/docs/5.7 really does make things easier.  For instance, cross site scripting functions are built in if you are going to be doing any forms processing.   It is worth the time to spend a day reviewing https://laracasts.com/ where there is some good basic php info worth looking at https://laracasts.com/series/php-for-beginners and OOP https://laracasts.com/series/object-oriented-bootcamp-in-php.  While not required, they are worth the view.  There is a tutorial on building an app from scratch https://laracasts.com/series/laravel-from-scratch-2018 that will help make you feel more comfortable to using this library https://laracasts.com/series/laravel-from-scratch-2018

If you try and roll your own or use full framework like laravel, it comes down to pointing your traffic to your index page, then routing your variables to a template page.

In regards to Wordpress, it is also a framework. If your site will need updates by users, it is worth looking into more because it is a CMS. You don't have to use plug ins for things like forms and you can process your own. It is just a matter of understanding the wordpress way of doing things https://premium.wpmudev.org/blog/how-to-build-your-own-wordpress-contact-form-and-why/

Which way you go will depend on your app.  If it is heavy on content management, then WP is worth taking a second look and learn to do custom development for WP.
skullnobrainsCommented:
for #2 the code for the title would be something like

switch(basename($_PHP_SELF)){
  case 'pageX.php': echo "my nice title" ; break ;
  case 'pageY.php': echo "another title" ; break ;
  default: echo "most likely the site's name here" ;
}

a similar statement will deal with keywords.

a #3 alternative might be to use a couple of switch statements to populate variables in the header.php file or a separate config file before printing the headers which is easier to maintain.

a #4 alternative could be to always call the same page which deals with the headers and basic site structure with a parameter such as $page='whatever' and use that $page parameter to include the proper page, set of keywords, and title.


i would not recommend using a framework before you have experience with manually coding a simple site.
i would not recommend a framework for such a simpe task.
that said wordpress once setup will allow non power users to update the site's content quite easily, and falcon actually brings something to php. other frameworks are in my opinion mostly ways to make your site more complex to code, more complex to debug, more complex to deploy, ... and immensely slower.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.