website template / using $_POST and $_GET from included file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
@modi
Forum Newbie
Posts: 3
Joined: Sun Mar 18, 2007 11:18 pm

website template / using $_POST and $_GET from included file

Post by @modi »

I wanted to apply a certain structure/template to every page in my website. The way it is set up now is with an index.php file taking GET requests and returning the corresponding page:

index.php:

Code: Select all

...
$return_page = "";
switch( $_GET["page"] ) {
   case "home":
      $return_page = "home.php";

   case "register":
      $return_page = "register.php";

   case "about":
      $return_page = "meta/about.php";

   default:
      "http404.php"
}

include_once($return_page);
...
The problem with this setup is that I can't use $_GET or $_POST from the page included by index.php. For example, I sometimes need to retrieve a post request in register.php. I either need to retrieve $_GET or $_POST requests from an included file, or I need a way to apply the template individually to each file and then have each page requested directly.

I tried the following but it didn't work:

index.php:

Code: Select all

<html>
<body>
...

<?php
   function applyTemplate($content) {
      echo $content;
   }
?>

...
</body>
</html>


register.php:

Code: Select all

<?php include_once("index.php");?>

<?php
$content = '
...
';

applyTemplate($content);
?>
The returned page had the content from register.php appended to the content from index.php (instead of being embedded in the content from index.php, which is what I wanted).

I don't know how to get around this. If you can, please help.
@modi
Forum Newbie
Posts: 3
Joined: Sun Mar 18, 2007 11:18 pm

Post by @modi »

I just realized $_GET and $_POST are global variables, so I can always access them from an included file. I do still have the problem of having a form with method="GET", because it prevents having a central template file that takes page requests via GET and returns the corresponding page, as coded below. Is there a way to apply a template to an entire website without doing the following?:

Code: Select all

...
$return_page = "";
switch( $_GET["page"] ) {
   case "home":
      $return_page = "home.php";

   case "register":
      $return_page = "register.php";

   case "about":
      $return_page = "meta/about.php";

   default:
      "http404.php"
}

include_once($return_page);
...
Basically, I want each page to be requested directly and apply the template from within each page using a call like

Code: Select all

applyTemplate($pageContent);
Post Reply