We help IT Professionals succeed at work.

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

nikez2k4
nikez2k4 asked
on
Medium Priority
240 Views
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?

Thanks,
MOZ
Comment
Watch Question

Senior Software Developer
Commented:
<?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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.