Facebook debugger scraper doesn't understand PHP include - php

This is a weird one...
I have various php pages which includes head.php to display header tags, for example:
certificate.php
<html>
<head>
<?
$SEO_image = "custom_image.png";
include_once "head.php";
?>
</head>
...
</html>
head.php
...
<?
if(!isset($SEO_image)) $SEO_image = "default_image.png";
?>
...
<meta property="og:image" content="<?echo $SEO_image; ?>">
<meta property="og:image:width" content="200px">
<meta property="og:image:height" content="129px">
....
I include the head.php in many pages and when I load the site in a browser everything works according to plan. If the parent page first instanciates $SEO_image before the include_once "head.php" the page source shows me the correct image custom_image.png. Otherwise shows me the default_image.png.
But if I run the same url which in the browser displays custom_image.png by facebook's debugger scraper it says the og:image tag is default_image.png. This happens with all tags that use variables in the same way (first declared in the parent page to used inside the included php script).
Facebook scraper: https://developers.facebook.com/tools/debug
It's like if facebook's scraper is doing the impossible and somehow disassociating the variables in the parent pages from the head.php page.
What do you think can be happening here?
Thank you!
NOTE: I am aware that the og:image tag requires the entire url. I edited the code to exclude the site. As I mentioned, when viewing the page sources in a browser everything works properly.

Related

Creating dynamic title tags for each page in an included header.php file.

I've looked all over and even watched a simple Youtube video on how to create dynamic titles for each page cause you don't want each page having the same title, right? Right.
So in my header.php file I've created a variable between the head tags like so:
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
<title><?php echo ['$pagetitle']; ?></title>
And of course in one of my index.php files I've defined the variable like so:
<?php $pagetitle = 'some-page-title'; ?>
It can't get any simpler than this by creating a variable and defining what that variable will be called. But it seems this isn't flying.
I'm using PHP5.6 in XAMPP for testing purposes, and I also use Netbeans which isn't correcting me for the way I created the PHP code; and I'm getting a "undefined variable" page_title and an ARRAY to string conversion in the header.php file. I have defined the variable so why am I getting an array error thrown at me? There must be something I'm missing cause I've looked closely at everything.
Thanks for taking the time to view and answer!
I did exactly that for my projects.
index.phtml
<html>
<head>
<?php
$title = "Some title";
require_once("includes/header.php");
?>
</head>
<body>
<!-- whatever code ... -->
</body>
</html>
includes/header.php
<?php
echo "<title>$title</title>\n";
# something else if needed...
?>
Works just fine.

PHP Include <head> and Social Media tags

I created a family website. I have the header for the entire site all the way through my main as a PHP INCLUDE. This is a must because I have a massive directory of fly down menus that need to change across the site when I change that one file.
Everything works beautifully except for one thing: I have unique articles where only on those pages I need unique meta tag OG information specific to that one page so the correct information is displayed when the article is shared on Facebook, Twitter, etc. When you use a PHP include across the entire site for the header, how do you change the unique meta OG data for select pages only?
I know putting a 2nd head tag wouldn't be ideal. Also, I would have to put it in the body given my include file for the header includes the top body tag.
Before I went and changed the coding logic of the site, I was hoping there is a straightforward solution to this I am no aware of (and I am a noob coder so that may be part of it).
I know I could put the main menu only in a file, and then call that menu file in a 2nd header file that is then itself called in an include across the entire site.. that would give me the latitude in those pages. But was hoping there is an easier answer like, "oh no, they meta OG tags can go in the body like this...." or something?
I would recommend setting the desired header information in PHP variables at the top of the page, and then include the PHP header file:
page.php:
<?php
$og_url = 'http://www.example.com';
include('header.php');
?>
header.php:
<html>
<head>
<meta property="og:url" content="<?php echo $og_url; ?>" />
</head>
This way, you can set unique social media information for each page, while still making use of the global <head> section from the header file. Keep in mind that you'll probably also want fallback behaviour in case your variables aren't referenced on every page that you call the header file from:
<meta property="og:url" content="<?php echo isset($og_url) ? $og_url : $_SERVER['DOCUMENT_ROOT']; ?>" />
While a document has to start with <html>, it's perfectly fine to set PHP variables before calling <html>; it's only content that's actually written to the DOM that you need to worry about.
Hope this helps! :)

How to design independent header and footer page

I am new in web designing.my website is based on articles.I want that it must be searchiable by search engine and each page open with header and footer of site with it direct link example let I have article on WiFi_connectivity.html in directory root/article than link is
Mysite.com/article/WiFi_connectivity.html;
But point is that I don't want add header and footer in each page and page should have its original link for that article for bookmark or else
You would just include the header and footer files like so:
<?php
//set variables for your head tags here. title, keywords etc....
include('header.php');
//Your content goes here
include('footer.php');
?>
Your header.php file would look something like this:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title><?php echo $title; ?></title>
<meta keywords="<?php echo $keywords; ?>"
</head>
//the rest of your header.php code here
To keep it SE friendly do all of your code above the first include so you can add proper title, meta tags, etc to the header file. Then just name the page what ever relevant name you like and it will retain the dynamic title for bookmarks.
This is an overly simple version but it is just to give you a rough idea of where to start.

create and save pure html file from php 'template'?

I am looking for a simple and effective way to create a pure html file based off a php file. For instance, in template.php below the php would be inserting various portions of the page. I want to save the page then as html removing all php code and leaving what was inserted by it... hopefully that makes sense... the output of template.php would be a better way to say it I guess.
First, I do not know if something like this is possible. Second, is this the best way to go about something like this?
Before anyone starts screaming about security there will be ZERO user submitted / form submitted variables in this page. My goal is to create a report from database values with the template which the user can then view/print/save off the server as pure html. There will be no images only inline css.
EDIT :
This html only output of template.php needs to be saved on the server as its own file. The reason for the php 'template' is because I will be creating the vast majority of the page with php... but I only want to save its resulting output.
template.php :
<!DOCTYPE html>
<html lang="en">
<!-- BEGIN HEAD -->
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<meta content="<?php echo $desc; ?>" name="description" />
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body>
... further html with php mixed in
</body>
<!-- END BODY -->
</html>
Current solution :
I did some further research and this is acting exactly how I want it to. Comments/suggestions welcome for it.
<?php
ob_start();
require_once('/home/test/public_html/template.php');
if ( ob_get_length() > 0 )
{
$ssReport = ob_get_contents();
file_put_contents('/home/test/public_html/test.html', $ssReport);
}
ob_end_clean();
?>
You can use REGEX (Regular Expressions) to strip out all php-code.
The Expression <\?php.*?\?> can do that for you...
PHP-Example:
$tempateFile = file_get_contents('template.php');
$htmlPlain = preg_replace('/<\?php.*?\?>/si', '', $tempateFile);
If you allow "Shorthand Open Syntax" for php-files <? instead of <?php you should use the pattern <\?(?:php)?.*?\?> which will become preg_replace('/<\?(?:php)?.*?\?>/si', '', $tempateFile);
If you want to take a html-snapshot of your site(s) try this options:
use wget to grab a copy of your website (see wget manual or wget tutorial)
use curl with a script to grab a copy of your website (see curl manual or
curl tutorial
make a php-script which uses file_get_contents($url) to grab outputs of you specific public-available URL and store it with file_put_contents(...) to a html-file.

How does phpinfo() change the title of the page permanently?

If you use phpinfo() in your page, you will notice it will change the title of your page? And even if you give the title of your page to be different it will not change. How is this done?
Is because phpinfo print <title>, is depend on how browser handle the tag rendering.
Chrome, Firefox and internet explorer is practicing FIFO.
So, do this
<title>my title</title>
<?phpinfo() ?>
is my title being set as browser page title
<?phpinfo() ?>
<title>my title</title>
Then above will set page title to phpinfo()
phpinfo outputs a complete HTML page, including a <head> section with <title>phpinfo()</title>. You can only have one <head> and one <title> per page, so I wouldn't even know how you'd attempt to change the title to anything else without producing a completely broken page to begin with.
put <title> tag above phpinfo(). Check the page source there will be two title tags , first one will be shown.

Categories