Forum Moderators: coopster

Message Too Old, No Replies

Running PHP Embedded In a String

         

Relish

9:11 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



I am building a CMS. It doesn't have to be that secure, as it is remaining on localhost (my computer). It is all working fine: you can create templates, create pages, and apply that template to the page. You can add content to the pages, and everything is stored in a database. The content is retrieved from the database, along with the title, template, etc, and the content replaces where you add <contentarea /> in the template. The problem is, I want to be able to add PHP to the content area, but to get to the content area, it must be stored and retreived from a database. How would I replace <contentarea /> on the page in a manner that allows the PHP stored in the page to run?

The string retreived would look like this:


Text Text Text Text Text Text <?php echo "Text "; ?> Text Text Text Text

Etc.

How would I do this? Thanks!

[edited by: Relish at 9:11 pm (utc) on June 18, 2008]

londrum

9:19 pm on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



should be easy enough. if you store the contents of the page in a buffer (i.e. by using ob_start and ob_end_flush) before you serve it to the browser, then it should run the code.

you'd have to remember to return the characters back to what that were in the first place though, before you saved them in the database. (you probably changed '<', '>' and the like into the numbered equivalents.)

Relish

9:29 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



I've never done something like that before. How would I do this?

Thanks so much for the reply!

This is how the content goes into the page:


function DisplayBody(){
echo str_replace('<contentarea />', stripslashes($this->content), $this->body);
}

[edited by: Relish at 9:30 pm (utc) on June 18, 2008]

londrum

7:43 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try putting this at the top of your code (right at the very top of the entire php page -- the very first line)

<?php ob_start(); ?>

and this at the very end

<?php ob_end_flush(); ?>

everything between those two will be buffered, run in the normal way, and then outputted at the end.
if it doesn't work have a look at the source code. if you see any of the php code that was supposed to be running then you'll know that it hasn't written to the page correctly (probably something to do with the < and > signs when you pulled it out of the database)