开发者

How do I load content from a PHP file (and executing the script) from another PHP file?

I have a PHP script (index.php) that changes the page shown depending on the query part of the URL (using GET). All the links in the page access different "pages" using ?page=pageName and the ind开发者_如何学编程ex.php script loads HTML from text files accordingly (echoing it to the screen).

So, if a user goes to index.php?page=about, my PHP script loads a file called about.php and echoes it in a view container in the browser.

However, I have a big problem: The content I'm loading has PHP scripts inside it, but those scripts aren't executed when echoed using the index.php script. That makes it impossible for me to load a page that contains, say, a directory listing pulled from a MySQL database.

So basically, I want to know if there is any way that I can echo a PHP file onto a page but execute the PHP code inside the file as well.

For clarity, I have attached an annotated screenshot that describes what I mean:

EDIT: Here's the link:

How do I load content from a PHP file (and executing the script) from another PHP file?


Use if statements to include('about.php') in index.php, and put the echo statements inside about.php. If this doesn't work, please post code examples.

if( $_REQUEST['someQuery'] == 'about' )
{
     include('about.php');
}


Using include() will include the content of another PHP script and execute it. If that other PHP script contains functions that produce the output then you will need to invoke them separately.


Please don't start your sentences with conjunctives.

changes the page shown depending on the URL

Doesn't that happen with most web pages?

access different "pages" using ?page=pageName

Oh - you mean by the query part of the URL - a front controller pattern - yeuch!

I can echo a PHP file onto a page but execute the PHP code inside the file as well.

Do you mean you want to see the PHP source code and the output of the code? Or just the output of the code?

In your front controller:

<?php
    if (realpath($_REQUEST['page'])!=$_REQUEST['page']) {
        header("HTTP/1.1 500 Internal Server Error");
        die ("Naughty!");
    }
    $script=SOME_PATH . $_REQUEST['page'] . ".php";

    if (!file_exists($script) || !is_readable($script)) {
         header("HTTP/1.0 404 Not Found");
         die ("File not found");
    }
    print "src = <br />";
    highlight_file($script);
    print "<hr />Output:<br />";
    include($script);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜