+ Reply to Thread
Results 1 to 7 of 7

Thread: Store php code in sql and execute it?

  1. #1
    gluxon is offline x10Hosting Member gluxon is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    31

    Store php code in sql and execute it?

    I'm starting up my own CMS, so one of the problems I've come across is that I can't store php coding in a MySQL table and execute it. It'll show up as HTML.

    I realize I could write the whole thing to a file, then include it, but I'd like to see if there's another way other than that.

    Thanks.

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Store php code in sql and execute it?

    You can always try to eval( $code ) where $code is the php code you stored in the database.

    But including a file has much less overhead than 1) making a database query and then 2) eval() the result.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Store php code in sql and execute it?

    Quote Originally Posted by dlukin View Post
    But including a file has much less overhead than 1) making a database query and then 2) eval() the result.
    Not to mention that using eval will open up a potential injection vulnerability. It has its uses, but as Rasmus Lerdorf (PHP's inventor) once said,
    If eval() is the answer, you're almost certainly asking the wrong question.
    Of course, writing to a file & including it introduces the same vulnerability, and has even more overhead. OP, I have to ask: why do you need to evaluate arbitrary PHP code?
    Last edited by misson; 06-05-2010 at 12:51 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    gluxon is offline x10Hosting Member gluxon is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    31

    Re: Store php code in sql and execute it?

    Quote Originally Posted by misson View Post
    why do you need to evaluate arbitrary PHP code?
    So I can have php in pages :/

    For example, I used to use $BASE in my links and images, so the links would still work when I move the page (I realize I don't need to do this anymore with mod_rewrite and php). That wouldn't work when the page is placed in the database then executed.

  5. #5
    gluxon is offline x10Hosting Member gluxon is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    31

    Re: Store php code in sql and execute it?

    Among other things, this can be useful for storing code in a database text field for later execution. - http://php.net/manual/en/function.eval.php

    Looks like eval() was just what I needed :D

    Thanks dlukin and misson

  6. #6
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Store php code in sql and execute it?

    For what it's worth, storing PHP in a database for execution is a hideously bad way of getting around a problem. In no case should you ever have to do it.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Store php code in sql and execute it?

    A safer option is to use a limited template engine, storing strings in the template language. If you only need variable replacement, you can use preg_replace_callback to replace variable names with values.

    PHP Code:
    function lookup_var($matches) {
      if (
    count($matches) <= 2) {
        
    // simple variable syntax
        
    ...
        
    // e.g. "return $_GLOBALS[$matches[0]];", but that's still potentially unsafe
      
    } else {
        
    // complex variable syntax
        
    ...
      }
    }

    preg_replace_callback('/\$(?:(\w+)|{([^}]+)})/''lookup_var'$str); 
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Execute privilege
    By Journeyman in forum Free Hosting
    Replies: 0
    Last Post: 08-11-2008, 06:41 AM
  2. Cannot execute this PHP code...
    By anuj_web in forum Programming Help
    Replies: 4
    Last Post: 04-12-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers