SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 1st, 2003, 03:36 PM
mattp23 mattp23 is offline
Moderated
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 82 mattp23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 44 sec
Reputation Power: 5
Unhappy executing php code retrieved from db

I have the contents of several pages stored in a database, when a page is accessed, php code grabs the appropriate content and displays it between (included) header and footer code. For example, content code is grabbed from db and stored in $page_content:
PHP Code:
include 'include/header.php';
echo 
"$page_content";
include 
'include/footer.php'

This works fine when the content is HTML, how ever i was trying to include some PHP code, to be executed before the page is displayed,
for example
PHP Code:
if $page_content is "<?php echo 'test' ?>"
then the outcome should be:
....
whatever header ...
test
... whatever footer ...

however the output is:
....
whatever header ...
<?
php echo 'test' ?>
... whatever footer ... 

Obviously not what i was after, does anyone have any idea how to retrive code from a db and then execute it?

Thanks

Reply With Quote
  #2  
Old October 1st, 2003, 03:57 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
This seems like a rather dangerous thing to want to do. For example, what if a system call that attempts to remove all files in the current directory finds its way into your database? If you must do something like this, I'd consider mapping a series of acceptable commands to tag values and scanning the db results for the tags. If they exist, then execute the code using your defined routines for executing it rather than allowing arbitrary code to be executed.

Reply With Quote
  #3  
Old October 2nd, 2003, 12:17 PM
mattp23 mattp23 is offline
Moderated
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 82 mattp23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 44 sec
Reputation Power: 5
solution

just in case anyone is interested i have found a solution:

the eval function:
PHP Code:
eval ( ' ?>' $page_content '<?php ' ); 

this first escapes to html, incase the contents of $page_content is just a string with no code, then evauates $page_content, then returns to php.

Note any code stored in $page_content should be held within php tags, ie
PHP Code:
 $page_content 'string'// valid string
$page_content '<?php echo 'string' ?>'// valid code
but
$page_content 
'echo "string";' // is a string *not* code 

Hope this explains the solution, take into account the warning from dhouston. Thanks to kode_monkey for help with this solution.

Last edited by mattp23 : October 2nd, 2003 at 12:21 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > executing php code retrieved from db


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway