Skip to content
PHP Logo

How can PHP be used to dynamically generate and include metatags based on different criteria, such as the origin of the page request?

To dynamically generate and include metatags based on different criteria such as the origin of the page request, you can use PHP to detect the criteria and then dynamically generate the appropriate metatags before including them in the HTML head section. This can be achieved by using conditional statements to check the criteria and generate the corresponding metatags accordingly.

php
<?php
// Detect the origin of the page request
$origin = $_SERVER['HTTP_REFERER'];

// Generate metatags based on the origin
if ($origin == 'http://example.com') {
    $meta_tags = '<meta name="description" content="Metatags for example.com">';
} elseif ($origin == 'http://anotherexample.com') {
    $meta_tags = '<meta name="description" content="Metatags for anotherexample.com">';
} else {
    $meta_tags = '<meta name="description" content="Default metatags">';
}

// Include the generated metatags in the HTML head section
echo '<head>';
echo $meta_tags;
echo '</head>';
?>