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.

4 Likes

@Hyperbytes AKA Mr MySQL, could I have made this into a single database connection using SQL replace on the 2 fields that needed it? Then only had a single statement to worry about and no php if statements to contend with.

Hmm, why did you need to use custom code here rather than a server action?
Is it because you need to filter on the URL?

Some social media like facebook/twitter do not yet render js template and {{expressions}} in the meta tags :slight_smile: so they won’t display them properly when shared there.

1 Like

No, i was retrieving all this data initially using standard Server Connect and App Connect which is far simpler however with the javascript they use and the way facebook as an example parses page content they always see the data as {{my-page-title}} rather than the actual rendered title, as is the same case when using services like google site schema testing service.
So for meta data unless you are going to use something to prerender the javascript and serve a static HTML page back to social engines then you are kind of forced to go this route.

EDIT: Google search engine is smart enough to handle this correctly, its really more the social platforms that are so antiquated they cant figure it out.

I have some difficulty loading the database
I have tried various solutions but I always get errors that prevent the page from being displayed
Wanting to simplify to the minimum, in order to coexist with a Wappler page, how should PHP code be set before HTML?
Where am I wrong?



The php code should be BEFORE the tag, cant tell from your screenshot. What are the errors?

Yes the code is before html and close after /html
But with this code the page not work and Google report HTTP ERROR 500

A 500 error is a generic error. You need to show us the actual error via this procedure

In my honest opinion one thing i would try is before
$servername = "localhost"
Add another line and store your $_GET inside it’s own php variable, then call the variable in the $stmt rather. Right now you are kind of mixing languages together if that makes sense, and the AppConnect language and bindings would only work inside either a <body is="dmx-app"> or <html is="dmx-app">

So first test by trying a simple <?php echo $_GET['id']; ?> and see if anything is even output, i have a feeling it won’t.

Good idea Paul

try adding (in the parameters section)
$id = $_GET['id'];

change your select statement to:

"SELECT * FROM products where idprod =" . $id

1 Like

Not to confuse anything but just something for my own learning too is there any difference between these two ways of writing it @Hyperbytes or would it make no difference as such. I always try use the most semantically correct way, and yours looks cleaner, so just checking.

"SELECT * FROM products WHERE idprod = '$id'"

the way i would have written it vs your way of

"SELECT * FROM products where idprod =" . $id

Just went with code i have on an existing live page so I know it works, 100% sure

Should both work

EDIT: i hate nesting quote types if that can be avoided, just one of my many coding peculiarities

1 Like

With your advice, the page now opens regularly, but the database call


gives me an error in the Facebook Debugger

If set like this,
Schermata 2020-07-06 alle 15.18.17
it is OK but of course it only considers the first record it finds

I tried in every way, but I absolutely cannot get Facebook sharing to work dynamically

@Marzio could you share a link to the page so we can try it inside the facebook debugger please.

The tutorial was based on my own website, and it is used as I showed every day and seems to work fine in facebook, please take a look at one of my pages, sorry it kept changing the link so you will have to copy and paste it in

https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Fwww.africacollection.com%2Fthe-seychelles-travel-itinerary-packages%2Fseychelles-purely-beach-holiday-travel-itinerary%2F

To better verify the problem I am recreating the procedure on a currently unused domain
If I still encounter the problem, it will be easier to locate the error
I will give you the link to the page as soon as completed

1 Like

I created a test connection on vacanzemarine.it domain
This is the file that lists the contents of the database
https://www.vacanzemarine.it/test/index.php

This is the test page (see code source)
https://www.vacanzemarine.it/test/test_fb.php

This is the Facebook Debugger page (with code = 1) that doesn’t work
https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Fwww.vacanzemarine.it%2Ftest%2Ftest_fb.php%3Fprod%3D1