<p>I'm trying to write a page that calls PHP that's stored in a MySQL database. The page that is stored in the MySQL database contains PHP (and HTML) code which I want to run on page load.</p>

<p>How could I go about doing this?</p>

[{'answer_id': 41410, 'author': 'Vegard Larsen', 'author_id': 1606, 'author_profile': 'https://Stackoverflow.com/users/1606', 'pm_score': 2, 'selected': False, 'text': '<p>You can look at the <a href="http://php.net/eval" rel="nofollow noreferrer">eval</a> function in PHP. It allows you to run arbitrary PHP code. It can be a huge security risk, though, and is best avoided.</p>\n'}, {'answer_id': 41411, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 6, 'selected': True, 'text': '<p>You can use the <a href="http://php.net/manual/en/function.eval.php" rel="nofollow noreferrer">eval</a> command for this. <strong>I would recommend against this though, because there\'s a lot of pitfalls using this approach.</strong> Debugging is hard(er), it implies some security risks (bad content in the DB gets executed, uh oh).</p>\n\n<p>See <a href="https://stackoverflow.com/questions/951373/when-is-eval-evil-in-php">When is eval evil in php?</a> for instance. Google for Eval is Evil, and you\'ll find a lot of examples why you should find another solution.</p>\n\n<p>Addition: Another good article with some references to exploits is <a href="http://www.sitepoint.com/blogs/2005/02/27/eval-is-dead-long-live-eval/" rel="nofollow noreferrer">this blogpost</a>. Refers to past vBulletin and phpMyAdmin exploits which were caused by improper Eval usage.</p>\n'}, {'answer_id': 41440, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 3, 'selected': False, 'text': '<p><code>eval()</code> function was covered in other responses here. I agree you should limit use of <code>eval</code> unless it is absolutely needed. Instead of having PHP code in db you could have just a class name that has method called, say, <code>execute()</code>. Whenever you need to run your custom PHP code just instantiate the class of name you just fetched from db and run <code>-&gt;execute()</code> on it. It is much cleaner solution and gives you great field of flexibility and improves site security significantly.</p>\n'}, {'answer_id': 385542, 'author': 'dkretz', 'author_id': 31641, 'author_profile': 'https://Stackoverflow.com/users/31641', 'pm_score': 1, 'selected': False, 'text': "<p>Have you considered using your Source Control system to store different forks for the various installations (and the modules that differ among them)? That would be one of several best practices for application configuration I can think of. Yours is not an unusual requirement, so it's a problem that's been solved by others in the past; and storing code in a database is one I think you'd have a hard time finding reference to, or being advised as a best practice.</p>\n\n<p>Good thing you posted the clarification. You've probably unintentionally posed an answer in search of a suitable question.</p>\n"}, {'answer_id': 1472666, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<p>Easy:</p>\n\n<pre><code>$x // your variable with the data from the DB\n&lt;?php echo eval("?&gt;".$x."&lt;?") ?&gt;\n</code></pre>\n\n<p>Let me know, works great for me in MANY applications, can\'t help but notice that everyone is quick to say how bad it is, but slow to actually help out with a straight answer...</p>\n'}, {'answer_id': 18580803, 'author': 'Khaptin', 'author_id': 2741070, 'author_profile': 'https://Stackoverflow.com/users/2741070', 'pm_score': 0, 'selected': False, 'text': '<p>How I did this is to have a field in the database that identified something unique about the block of code needing to be executed.  That one word is in the file name of that code.  I put the strings together to point to the php file to be included.  example:</p>\n\n<pre><code>$lookFor = $row[\'page\'];\n\ninclude("resources/" . $lookFor . "Codebase.php");\n</code></pre>\n\n<p>In this way even if a hacker could access you DB he couldn\'t put malicious code straight in there to be executed.  He could perhaps change the reference word, but unless he could actually put a file directly onto the server it would do him no good.  If he could put files directly onto the server, you\'re sunk then anyway if he really wants to be nasty.  Just my two cents worth.</p>\n\n<p>And yes, there are reasons you would want to execute stored code, but there are cons.</p>\n'}, {'answer_id': 33864063, 'author': 'ashkufaraz', 'author_id': 634146, 'author_profile': 'https://Stackoverflow.com/users/634146', 'pm_score': 0, 'selected': False, 'text': '<p>Read php code from database and save to file with unique  name and then include file \nthis easy way for run php code and debug it.</p>\n\n<pre><code>$uniqid="tmp/".date("d-m-Y h-i-s").\'_\'.$Title."_".uniqid().".php";    \n$file = fopen($uniqid,"w");\nfwrite($file,"&lt;?php \\r\\n ".$R[\'Body\']);\nfclose($file);                          \n// eval($R[\'Body\']);\ninclude $uniqid;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/41406', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3654/']