Getting page content (Function calling)

Hi,
I'm making a test php website, and I have a main page ('template' - see below) with a call to include another page within it ('content_page1'):

<HTML>
<BODY>

<!-- Function getContent -->
<?php
    function getContent() {
        if (empty($_GET['page'])) {
            $page = 'content_page1';
        } else {
            $page = $_GET['page'];
        }
        $page .= '.php';
         
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);
    }
?>                              
                         
            <!-- CONTENT -->
            <?php getContent(); ?>
</BODY>
</HTML>

Question 1: How to grab a different content (''content_page1.php', 'content_page2.php', 'content_page3.php', etc1), but using the same 'template', main page?
Question 2: Where the functions should be placed: in the header or body of the page?
Question 3: Is it possible to separate the functions on the separate file?

Thanks!
Le
                                    
athanasius296Asked:
Who is Participating?
 
hernst42Connect With a Mentor Commented:
q1: this is alredy implemented an depengs on the paramters of url you are calling if it is e.g
template.php?page=content_page2 the file content_page2.php will be included/displayed. If page is not set content_page1 will be used.

q2: depends of the output of the included files (e.g if no body or header-tags are outputed use it inside the body-tag). but typical it should be placed in the body.

q3: yes you can put the function in a file e.g myfuncs.inc.php
and then include then in your php-page:

include 'myfuncs.php';

The whole thing will then look like:
template.php
<HTML>
<BODY>

<!-- Function getContent -->
<?php
include 'myfuncs.php';
?>                        
                     
            <!-- CONTENT -->
            <?php getContent(); ?>
</BODY>
</HTML>

myfuncs.php:
<?php

    function getContent() {
        if (empty($_GET['page'])) {
            $page = 'content_page1';
        } else {
            $page = $_GET['page'];
        }
        $page .= '.php';
         
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);
    }
0
 
Diablo84Commented:
Your function is very insecure, using it as it is i can include any file i want via the query string which could include files containing sensitive data, you should limit the inclusion options using a more complex if statement or a switch statement.

With regards to your questions...

1 << I'm not 100% sure what you mean but i think you will be wanting to put the function in a seperate file and include that file within other pages on your site. eg. put the function in a page called content.php and include it in all other files using include('content.php');

Pages can be dynamically loaded using domain.com?page=content_page1.php (changing the value (content_page1.php) for each).

2 << It doesnt matter as long as you call the function (ie. <?php getContent(); ?> ) where you want the page to display

3 << See 1
0
 
Diablo84Commented:
Example of safer way to write function:

      function getContent() {
       switch ($_GET['page']) {
            case("content_page1"):
                  $page = 'content_page1';
            case("content_page2"):
                  $page = 'content_page2.php';
            case("content_page3"):
                  $page = 'content_page3.php';
            default:
                  $page = 'content_page1.php';
       }
      }
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Diablo84Commented:
$page = 'content_page1'; should be $page = 'content_page1.php';

but you can see the idea
0
 
hernst42Commented:
It's not so insecure as only any file ending with '.php' can be included.
$page=/etc/passwd will not work as .php is added to the included fiilename, but checking that input should be done.
0
 
Diablo84Commented:
True but it still has the potential to cause problems, as a minimal example:

if index.php contained the line

<?php
include($_GET['page'].".php");
?>

and the query string was page=index

It will most likely result in an internal server error
0
 
athanasius296Author Commented:
Hi All,

I especially appreciate answer given by hernst42, but I have another question:
what if I include on the same 'template' page, more then one function, similar to the function <?php getContent(); ?> ?
For example, in addition to function getContent, on the same 'template' there is function getHeader (given below). Then, if I call: template.php?page=content_page2,
page 'content_page2' will be displayed in the Content and Header as well.
How to fix that?
Thanks!
Le


<!--Function getHeader-->
<?php
    function getHeader() {
        if (empty($_GET['page'])) {
            $page = 'header';
        } else {
            $page = $_GET['page'];
        }
      $page .= '.php';
         
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);
    }
    ?>


0
 
Diablo84Connect With a Mentor Commented:
use a different variable for the header

eg: query string

domain.com?header=onepage&page=anotherpage

then


<?php
    function getHeader() {
        if (empty($_GET['header'])) {
            $page = 'header';
        } else {
            $page = $_GET['header'];
        }
     $page .= '.php';
         
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);
    }
    ?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.