开发者

Processing PHP in a database

开发者 https://www.devze.com 2022-12-13 18:45 出处:网络
I have a basic cms that stores pages in a table in a mysql database - is it possible for me to include PHP in 开发者_开发技巧a page and then have PHP process it rather than just output it as-is?You ca

I have a basic cms that stores pages in a table in a mysql database - is it possible for me to include PHP in 开发者_开发技巧a page and then have PHP process it rather than just output it as-is?


You can use eval (http://php.net/manual/en/function.eval.php)

But remember that eval is evil


I think you'd have to use eval() to do such a thing. So yeah, possible but not recommended.


As both suggestions have indicated, using eval() is not recommended and poses a serious security issue.

Your best bet would be to create a basic templating system. You could have a pre-determined set of PHP code blocks on the frontend which are triggered by certain key values on the backend, i.e. {show_categories} could be a tag that when parsed, gets replaced with all categories.

To implement such functionality you would have to search for the particular template key values. If any such key values are found, run the associated code with that key value and replace the key with the code.

A very basic example of finding and replacing a template key:

// check if the show_categories key is found
if (strpos($body, '{show_categories}') !== false) {
    // generate the show categories output from a PHP function
    $categories = getCategoriesOutput();
    // replace key with content 
    str_replace('{show_categories}', $categories, $body);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消