Getting and executing (parsing) HTML and PHP code from a database

Posted on 2006-04-04
Last Modified: 2006-11-18
Hey guys,

I have a table that holds trusted page data in HTML and PHP, however I am having trouble trying to parse the PHP and then showing the complete page in the browser.

Just as an example, imagine I have this code in the table:

<p>This is some <strong>test</strong> text which includes some <?php echo "PHP";?></p>

I want to be able to echo this out onto my page, however as I said I cant seem to get the PHP pased.

I have tried simply getting the content and echoing the variable and I have tried using the eval() function but to no avail.

Can someone suggest a way I could do this that doesnt impact too much on performance?

Question by:nikez2k4
    1 Comment
    LVL 40

    Accepted Solution

    $s_string = <<< END_SOME_STRING
    <p>This is some <strong>test</strong> text which includes some <?php echo "PHP";?></p>
    echo "About to write the string to the work file\n";
    file_put_contents('dummy.php', $s_string);
    echo "About to inclue the workfile\n";
    include 'dummy.php';
    echo "\nJust included the work file\n";

    Outputs ...

    About to write the string to the work file
    About to inclue the workfile
    <p>This is some <strong>test</strong> text which includes some PHP</p>
    Just included the work file

    So it does work, but yeah, performance on this is a killer.

    UNLESS you cache it.

    As you write the data to the DB, use the row number to put the same data into a file OUTSIDE of webroot.

    Then when you need to include the code, get the list of IDs from the database and then include_once() the file.

    Sure lots of files, but less impact on the server as serving files via PHP is what it is doing.

    Having said that, you are going to have to be REALLY careful about the actual PHP code.

    It is VERY much harder to protect your code like this.

    Maybe the runkit functions could help here. Hmm. Maybe not.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now