0

I'm creating a PHP website that has one index.php file and instead of having files something.php, about.php, contact.php I have set it up as the following: index.php?p=something, index.php?p=contact etc.

Currently my index.php code looks like this:

<head>
   <title>Something</title>
   <link href="theme.css" rel ="stylesheet" media="screen">
</head>

<body>
<?php
if (@$_GET['p'] == 'something') include 'pages/something.php';
elseif (@$_GET['p'] == 'about') include 'pages/about.php';
</body>

Now the problem is that I want to have different metadata in index.php, something.php and about.php. I know I have to add metadata between <head></head>, but something.php and about.php are included later. How can I have different metadatas for the files? Is it possible to (re)set metadata after <head></head> tags?

unor
  • 92,415
  • 26
  • 211
  • 360
user3476448
  • 11
  • 1
  • 4
  • 1
    php can't reach into the past to modify output that has already occured. you'd either have to buffer your output so it doesn't get sent out at all, then modify the buffer, or use javascript to modify the `` once it reaches the client, or modify your script so there's multiple steps to generating a page, and you output the metadata at the right time/place. – Marc B Oct 28 '14 at 20:16

3 Answers3

1

in your index.php

<?php
if (@$_GET['p'] == 'something') include 'pages/something.php';
elseif (@$_GET['p'] == 'about') include 'pages/about.php';
?>

and in your about.php, something.php include the full html page included it's head tag

Marwelln
  • 28,492
  • 21
  • 93
  • 117
hassan
  • 7,812
  • 2
  • 25
  • 36
1

I do not know, do I understood your question, but of course, you can use your if condition in the header too!

<head>
    <title>Something</title>
    <link href="theme.css" rel ="stylesheet" media="screen">
    <?php
        if ($_GET["p"] === 'something') {
            ?>
    <!-- Write your metadata here if the page is something... -->

    <?php
        } elseif ($_GET["p"] === 'about') {
        ?>
    <!-- Write your metadata here if the page is about... -->
    <?php
        }
    ?>
</head>

<body>
    <?php
    if ($_GET['p'] == 'something') {
        include 'pages/something.php';
    } elseif ($_GET['p'] == 'about') {
        include 'pages/about.php';
    }
    ?>
</body>
vaso123
  • 12,347
  • 4
  • 34
  • 64
1

Try loading the metadata according to your preferences,as such:

<head>
   <title>Something</title>
   <link href="theme.css" rel ="stylesheet" media="screen">
   <?php if (@$_GET['p'] == 'something') echo "<meta charset='utf-8'>"; ?>
</head>

<body>
<?php
if (@$_GET['p'] == 'something') include 'pages/something.php';
elseif (@$_GET['p'] == 'about') include 'pages/about.php';
?>
</body>
umlal
  • 9
  • 2