PDO connection if you prefer not to use a prerender service

I decided for my newly created Africa Collection site that I would rather get the meta tags and other SEO tags filled with my own PDO connection because certain social media platforms and schema providers can not parse the javascript and just read the page source before it is rendered.

I have in the past used services from StackPath and prerender.io to produce similar results however this site changes pretty often, and the prerender indexing speed could become an issue for my client.

I needed a way to figure out what page was being viewed, I decided to use the canonical tag which is saved with each page in my database, as pages are created, to figure out what data needed to be returned.

The only page this was an issue for was the home page, as for the routing to work correctly I needed to make the home page have a URL, and therefore it is actually on a dummy URL of https://www.africacollection.com/home/ which i never want indexed nor seen by the general public, but it needs to be there for the routing.

So this is what I landed up doing, if anyone has any suggestions to streamline this, please feel free to comment, I am always open to correction.

<?php
	$canonurl = 'https://www.africacollection.com'.$_SERVER['REQUEST_URI'];
	$servername = "localhost";
	$username = "dummy_username";
	$password = "my_super_long_and_complex_password";
	$dbname = "prefix_suffix";

	try {
		$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		//The SQL statements
		$stmt = $conn->prepare("SELECT pc_canonical, pc_original_source, pc_title_main, pc_description_main, pc_social_title, pc_twitter_description, pc_twitter_image_alt, pc_facebook_description, pc_social_image_url FROM page_creation_tbl WHERE pc_canonical = '$canonurl'");
		$stmt2 = $conn->prepare("SELECT pc_canonical, pc_original_source, pc_title_main, pc_description_main, pc_social_title, pc_twitter_description, pc_twitter_image_alt, pc_facebook_description, pc_social_image_url FROM page_creation_tbl WHERE pc_canonical = 'https://www.africacollection.com/home/'");
		
		$stmt->execute();
		$mySeoTags = $stmt->fetch();
		
		if (empty($mySeoTags)) {
			$stmt2->execute();
			$mySeoTags = $stmt2->fetch();
		}
	}
	catch(PDOException $e) {
		echo "Connection failed: " . $e->getMessage();
	}
?>

This was placed above everything else on my page, before the opening html element.

Then inside my head element I switched all my meta data tags over to get the correct data from the results of whatever query ran, like this.

<title><?php echo $mySeoTags[pc_title_main]; ?></title>
<meta name="description" content="<?php echo $mySeoTags[pc_description_main]; ?>">
<link rel="canonical" href="<?php if($mySeoTags[pc_canonical] == 'https://www.africacollection.com/home/') { echo 'https://www.africacollection.com/'; } else { echo $mySeoTags[pc_canonical]; } ?>" />
<meta name="original-source" content="<?php if($mySeoTags[pc_original_source] == 'https://www.africacollection.com/home/') { echo 'https://www.africacollection.com/'; } else { echo $mySeoTags[pc_original_source]; } ?>" />
<meta name="twitter:title" content="<?php echo $mySeoTags[pc_social_title]; ?>">
<meta name="twitter:description" content="<?php echo $mySeoTags[pc_twitter_description]; ?>">
<meta name="twitter:image" content="<?php echo $mySeoTags[pc_social_image_url]; ?>">
<meta name="twitter:image:alt" content="<?php echo $mySeoTags[pc_twitter_image_alt]; ?>">
<meta property="og:title" content="<?php echo $mySeoTags[pc_social_title]; ?>">
<meta property="og:description" content="<?php echo $mySeoTags[pc_facebook_description]; ?>">
<meta property="og:url" content="<?php if($mySeoTags[pc_canonical] == 'https://www.africacollection.com/home/') { echo 'https://www.africacollection.com/'; } else { echo $mySeoTags[pc_canonical]; } ?>">
<meta property="og:image" content="<?php echo $mySeoTags[pc_social_image_url]; ?>" />
<meta property="og:image:secure_url" content="<?php echo $mySeoTags[pc_social_image_url]; ?>" />

And that was it, hopefully this helps someone else that may possibly have a need for something along these lines.

Community Page
Last updated: