• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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

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?

Thanks,
MOZ
0
nikez2k4
Asked:
nikez2k4
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
<?php
$s_string = <<< END_SOME_STRING
<p>This is some <strong>test</strong> text which includes some <?php echo "PHP";?></p>
END_SOME_STRING;
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";
unlink('dummy.php');
?>

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.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now