You are here: Re: evaluate code before serving it « PHP Programming Language « IT news, forums, messages
Re: evaluate code before serving it

Posted by Rik on 07/24/07 18:15

On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg =

<user@domain.invalid> wrote:

> Situation: I store news articles as individual PHP files. Each file
> contains HTML and now and then some embedded PHP snippets.
>
> Serving those news articles on the Web works fine, through include().
> But I want to also serve them through RSS, and found that in that case=

> the PHP source code is served -- it's not being evaluated.
>
> So it seems obvious that I need to first evaluate those PHP snippets,
> store the result in a variable, and use *that* as the input for the RS=
S
> generator script. I thought that'd be pretty straight-forward, but for=

> the life of me I can't figure out how to do it.

Depends on how you generator script works offcourse... Normally is =

shouldn't be a problem.

> I've experimented with eval() and ob_start() but either I misunderstan=
d
> how to use them, or they're not the right tools for this use.
>
> <http://de.php.net/manual/en/function.eval.php#76339> claims a solutio=
n,
> but it doesn't work for me. (I get no output whatsover using that
> approach.)

Simple eval solution:
The manual says: "To mix HTML output and PHP code you can use a closing =
=

PHP tag to leave PHP mode."

ob_start();
eval('?>'.file_get_contents('file.php').'<?php');
$string =3D ob_get_clean();


Then again, that would basically be the same as:
ob_start();
include('file.php');
$string =3D ob_get_clean();

Which is preferable.

It could be that your RSS-generator only swallows files/urls, and not =

strings? In that case:

//get a unique filename in temporary dir
$file =3D tempnam();
//open file
$fh =3D fopen($file,'w');
//start buffer
ob_start();
//run file
include('file.php');
//write output to temporary file
fwrite($fh,ob_get_clean());
//...And serve the filename $file to your script.
//remove temporary file
unlink($file);

You might get into trouble when the script sets headers though...

More simple solution: if the RSS generator gets urls, just give him the =
=

exact url of the file holding the article.

As you can see, without knowing anything about your RSS generator we're =
=

flying blind here.
-- =

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация