0

I work with PHP includes, and I need to put HEAD information in one of them. Is this possible, or can I only put a HEAD section on top of the index.php?

I'm asking this because the PHP includes has queries which I need in order to get OG image data (for social media) into the head. For example: I have a file WEBSHOP.PHP and in this file there is a product with an image. I want that image to show on the timeline in FaceBook.

This is an example of my (shortened version) of index.php:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
  </head>

  <body>
    <? include webshop.php; ?>
  </body>

This is an example of my (shortened version) of webshop.php:

<!-- some mysql query to get variables as $pic and $row->meta_title -->

<head>
  <meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
  <meta property="og:title" content="<? echo $row->meta_title;  ?>" />
  <meta property="og:description" content="<? echo $row->meta_des; ?>" />
  <meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo  $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo  $url_array[2] ; } ?>" >
</head>

<!-- some code to view the webshop item -->
9
  • You can put HEADs anywhere before content is written. Commented Dec 10, 2014 at 17:33
  • Show us some code. What have you tried already. Commented Dec 10, 2014 at 17:33
  • I have added the code in my original post. Commented Dec 10, 2014 at 17:37
  • @user3147480 Add your code to the question where it is more readable. You also need to make clear your are talking about the html <head> elemenet and not the header responses as would be returned for HTTP HEAD request. Commented Dec 10, 2014 at 17:39
  • The answer to your question is that, yes, you can use PHP to dynamically include this in any page you want. You can use PHP to output any part of the HTML document you desire. What specific problem are you having in doing so? Commented Dec 10, 2014 at 17:42

2 Answers 2

1

You're going to have to change the structure of your PHP files a bit in order to get all the header tags into one <head> section. If you include the webshop.php file before you start generating your HTML output you can then access the PHP variables when you write the head section. Something like this:

index.php:

<?php include webshop.php; ?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <meta property="og:title" content="<?php echo $row->meta_title;  ?>" />
    <!-- other meta tags using variables from webshop.php -->
</head>
<body>
    <!-- print out HTML code from webshop.php -->
    <?php echo $doc_body; ?>
</body>

Then in webshop.php you'll have to save any HTML output with output buffering so you can add it into the HTML code in the proper place. Something like this:

<?php
    // sql queries to get data

    ob_start();
?>
<!-- html code to show up in the body section to view webshop items -->
<?php
    $doc_body = ob_get_clean();
?>

Check out the PHP.net manual page on Output buffering for more info on ob_start and ob_get_clean.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes you can. However this is bad style. And you are making your HTML wrong:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
</head>
<body>
    <? include webshop.php; ?>
</body>

this will lead into

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
</head>
<body>
    <head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title;  ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo  $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo  $url_array[2] ; } ?>" >
</head>
</body>

However HTML does not like that the head tag is inside of the body tag. But most browser will still show it correctly. To be sure: Check your result with a HTML Validator.

4 Comments

This is exactly what I mean: The HTML is wrong like this and Facebook won't understand it. So I must change this in some way in order to get this working in FaceBook.
Yes, you should change your call. Otherwise you can't join it with other PHP tools. Modify your SHOP PHP so it does not output its own header. Or separate your PHP, one for the header and one for the body tag.
The problem is that the query is called in webshop.php, so I can't put it in the indexfile. Do you suggest seperate HEADER includes then? For example: <head> <? if ($nav == 'webshop') { include header-webshop.php ; } elseif ($nav == 'news') { include header_news.php ; } ?> </head>
Yes, the different header includes would be an easy solution. It would however be better to do everything inside of a index.php file. Do not mix HTML with PHP so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.