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
LVL 1
nikez2k4Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
<?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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.