Re: Executing PHP code in a database

From: Date: Sat, 08 Mar 2003 13:28:03 +0000
Subject: Re: Executing PHP code in a database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-138749@lists.php.net to get a copy of this message
At 06:15 08.03.2003, Leo Spalteholz said: --------------------[snip]-------------------- >I have a table that stores all the "boxes" that my website will >display on the sides. So far the content is simply stored in a text >field and then substituted for the box body. However this way I >can't have any php code in the boxes... What I thought I could do is >have another field that will contain any php code that I want for >that box and then execute that after I read it from the database. > >So I have two questions basically. >1. How would I go about executing php code in a string? >Say I have $code = "md5('blah');" how would I execute the code in >the string? To execute any code contained within a string you need to eval() it. Assuming you have this piece of code $uname = 'Leo'; $text = "<b>Hello there, <?php echo \$uname; ?></b>"; eval("?>$text<?php ;"); Note that you first escape from PHP in order to correctly "execute" plain text. >2. If I instead include a file would it have access tio the local >variables? > >Ie. In a class I have this: >$strContent = "blah, this is the content I got from the database"; >include("includefilefromdatabase.php"); > >Is the $strContent variable then available from the include file? Yes. See http://www.vogelsinger.at/test.php for an example (will be available for 1-2 days). -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#138749) next »