Can I "include" text that isn't a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cornrow
Forum Newbie
Posts: 3
Joined: Fri Sep 07, 2007 3:49 pm

Can I "include" text that isn't a file?

Post by cornrow »

For an application I'm writing I have different versions of a page stored in a database. I want to have a few lines of PHP and then spit out the correct version from the database.

If I generate an actual file for each version I can include it no problem. Is there something like include that I can use to import the code from the database.

I can't just echo because then PHP code doesn't get processed. I can't eval() because it doesn't like <?php ?> tags.

Any ideas?
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

I guess you can write it out to a temporary file and include that file. I just tried it and it works, but I have no idea how efficient this is.

/josa
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can eval().. it simply isn't recommended.
shwanky
Forum Commoner
Posts: 45
Joined: Thu Feb 15, 2007 1:21 am

Post by shwanky »

you can write the data to a php file on the file then include that php file on the fly? But why would you want to store php code in a database? Why not just store the php output in the database.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Strip the <?php tags with something trivial like:

Code: Select all

substr($buffer, 5, strlen($buffer)-7);
eval($buffer);
A database is not necessarily less secure than a file system, so eval() should be fine.
(#10850)
cornrow
Forum Newbie
Posts: 3
Joined: Fri Sep 07, 2007 3:49 pm

Post by cornrow »

Stripping out the php tags isn't really an option. There are some cases where there might be nested PHP in a textarea that isn't meant to be eval'd. I could write something complicated, but I'm not confident I can think of every possible case.

I'm building a multivariate testing platform, so I the input could be anything. Storing to files is fine, but then I have to deal with permissions and such. I guess if there's no clean option, that's what I'll do.

Tynan
Post Reply