{"id":3083,"date":"2013-03-14T18:59:14","date_gmt":"2013-03-14T18:59:14","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3083"},"modified":"2013-03-14T18:59:14","modified_gmt":"2013-03-14T18:59:14","slug":"easily-get-php-code-from-database-execute-parse-it","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/easily-get-php-code-from-database-execute-parse-it\/","title":{"rendered":"Easily Get PHP Code From Database &#038; Execute \/ Parse It"},"content":{"rendered":"<p>Do you have some <em><strong>custom PHP code in database that you wish to\u00a0retrieve\u00a0and parse \/ execute and show the output in the browser<\/strong><\/em>? Here&#8217;s an easy way to do just that.<\/p>\n<p><!--more--><\/p>\n<h2>How to\u00a0Easily Get PHP Code From Database &amp; Execute \/ Parse It<\/h2>\n<p><em><strong>One way to parse PHP code is to use &#8220;eval&#8221;<\/strong> <\/em>. So before we look at how to parse the PHP content from database, let&#8217;s look at the general way to parse PHP code using eval. Consider the following example:<\/p>\n<h3>Example 1: Parse \/ Execute PHP code in general<\/h3>\n<pre class=\"lang:php decode:true \">&lt;?php \n$fruit1 = 'Apples';\n$fruit2 = 'Grapes';\n$my_text = 'I like $fruit1 and $fruit2';\necho $my_text . \"&lt;br&gt;\"; \neval(\"\\$my_text = \\\"$my_text\\\";\"); \necho $my_text . \"&lt;br&gt;\"; \n?&gt;<\/pre>\n<p>When you execute the above code, you will see the following output:<\/p>\n<pre class=\"lang:php decode:true\">I like $fruit1 and $fruit2\nI like Apples and Grapes<\/pre>\n<p>So simply remove the first echo if you want to use it to parse general PHP code. Now let&#8217;s see how to use eval on PHP content from database.<\/p>\n<h3>Example 2: Get PHP code from database and\u00a0Parse \/ Execute it<\/h3>\n<p>Let&#8217;s say that you have some custom PHP code in database that you wish to run and show the output of. So consider the following code as stored in database:<\/p>\n<pre class=\"lang:php decode:true\" title=\"PHP code in database\">&lt;form method=\"post\" class=\"my_form_class\" id=\"my_form\" action=\"&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;\" &gt;\n\n&lt;\/form&gt;<\/pre>\n<p>Note the action attribute of the form. If you get this code from database and try to run it, you will see an error that breaks the code because the code from database contains the PHP start and closing tags. So to avoid that, you will need to modify our code a bit to make it work without breaking. So here&#8217;s how it can be done:<\/p>\n<pre class=\"lang:php decode:true crayon-selected\" title=\"Apply eval on content from database to execute the PHP code without breaking it\">$final_content = eval('?&gt;' . $database_content_with_php_code . '&lt;?php ');\n\necho $final_content;<\/pre>\n<p>Follow the above steps and you will see that the content can now be shown properly in browser.<\/p>\n<h4>NOTE:<\/h4>\n<p>Using &#8220;eval&#8221; is considered dangerous to use and not it is not recommended because of the vulnerabilities associated with it. Read more about eval <a title=\"Read more about why not to use eval\" href=\"http:\/\/php.net\/manual\/en\/function.eval.php\" target=\"_blank\">here<\/a>.<\/p>\n<h4>Your Turn!<\/h4>\n<p>Do you know of any other ways to <em><strong>get PHP code from database and parse \/ execute it<\/strong><\/em>? Please feel free to share your thoughts by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you have some custom PHP code in database that you wish to\u00a0retrieve\u00a0and parse \/ execute and show the output in the browser? Here&#8217;s an easy way to do just that.<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[33],"class_list":["post-3083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-NJ","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3083"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}