patternphpMinor
Facebook crawler redirect to force displaying own opengraph meta-tags
Viewed 0 times
opengraphforcefacebookmetadisplayingtagsownredirectcrawler
Problem
For my website, I use the opengraph to send the right data to the crawler.
On the top of all pages on my website, I include the opengraph-script, like this:
facebook-crawler.php:
Of course, the script would be extended for each link. If no function for the actual website is present, the home-page would be used.
Is this a legacy-
On the top of all pages on my website, I include the opengraph-script, like this:
if (preg_match("/facebookexternalhit/is", $_SERVER['HTTP_USER_AGENT'])) {
include("./facebook-crawler.php");
exit();
}facebook-crawler.php:
## Infos for Crawling
$server_path = $_SERVER['REQUEST_URI'];
$expl_path = explode("/", $server_path);
$page_title = $expl_path[1];
$page_data = "";
$page_path_display = "";
if (isset($expl_path[2]) && $expl_path[2]!="") {
$page_data = $expl_path[2];
$page_path_display = "/".$page_title."/".$page_data;
} elseif ($page_title!="") {
$page_path_display = "/".$page_title."/";
} else {
$page_path_display = "/";
}
function show_fbmeta_home() {
global $site_url, $site_url_displayname, $fb_app_id, $fb_admins, $page_path_display, $count_cars, $count_member, $count_ps;
get_statistics_general();
$use_title = "Lunatics Car Club Austria - est. 2014 - {Beta}";
$use_text = $count_member." Mitglieder, ".$count_cars." Autos, ".$count_ps."PS. Besuch unsere Website um mehr zu Erfahren!";
$use_type = "article";
$use_pic = $site_url."/images/home/r32-gruppenbild.jpg";
$meta_string =
'
';
return $meta_string;
}
// Meta Cases
switch($page_title) {
case "": case "home":
echo show_fbmeta_home();
exit();
break;
}
show_fbmeta_home();Of course, the script would be extended for each link. If no function for the actual website is present, the home-page would be used.
Is this a legacy-
Solution
-
It would be better to assign
-
You're missing whitespace in this statement (
-
In the following code block, you don't need to assign
for example:
It would be better to assign
$page_path_display this value ("/") from the beginning as all the cases use that as a prefix.-
You're missing whitespace in this statement (
$expl_path[2]!=""), it should be $expl_path[2] != "".-
In the following code block, you don't need to assign
$meta_string as you return it straight away.return
'
';- In the following code block, you could just use an
if-elseinstead of aswitch
switch($page_title) {
case "": case "home":
echo show_fbmeta_home();
exit();
break;
}for example:
if ($page_title == "" || $page_title == "home"){
echo show_fbmeta_home();
exit();
}global, you should avoid using globals, because they're bad practice.
Code Snippets
return
'<meta property="og:title" content="'.$use_title.'" />
<meta property="fb:app_id" content="'.$fb_app_id.'"/>
<meta property="fb:admins" content="'.$fb_admins.'"/>
<meta property="og:type" content="article" />
<meta property="og:url" content="'.$site_url.'/" />
<meta property="og:image" content="'.$use_pic.'" />
<meta property="og:site_name" content="'.$site_url_displayname.'" />
<meta property="og:description" content="'.$use_text.'" />
';switch($page_title) {
case "": case "home":
echo show_fbmeta_home();
exit();
break;
}if ($page_title == "" || $page_title == "home"){
echo show_fbmeta_home();
exit();
}Context
StackExchange Code Review Q#96959, answer score: 5
Revisions (0)
No revisions yet.