Problems with eval()

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
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Problems with eval()

Post by visionmaster »

Hallo together,

I'm at the moment at a dead end and don't have any idea where the mistake is!... :(

The strange thing is that my script works fine on one my test-server but dosn't work on the main server. Maybe some configuration possibility in php.ini or apache? Don't know if that is the correct direction to search for.

Here an explanation what my script actually does:

The concept makes possible the sepearation of php-code (logic) and html-code(design). My html templates hold variables e.g. <?=$strMeldung?>
These variables must be filled with content and be inserted in the html-template. The "filling" of the variables with content is done by php-modules, in this case "mod_auswahl.php".

1. Start with the modul "mod_auswahl.php".
2. In the modul "mod_auswahl.php" the function strReadTemplate() reads in the html-template "loeschen.template.html" and returns die read in html-page as a string.
3. The fuction eval() evaluates the string, as if it was php-code, that is executes the string as php. Since eval() begins in „php-modus“ the string that is to be evaluated begins with ?> (php ends),
$strTemplate holding a html-template. „?>“ provides the function eval() with the expected php-code at the beginning. Herewith changing from PHP-modus to HTML-modus.
=>Recapitulatory the place holders (=variables) in the html-templates will be filled up with values. At the end the place holder $strMeldung holds the value "foobar".

Thanks for your help!


mod_auswahl.php:
----------------

Code: Select all

<?php

include "mod_templates.php";

$strMeldung = "foobar";

$strTemplate = strReadTemplate("loeschen");
$strOutput =     eval ( "?>".$strTemplate."<?");
echo $strOutput;

?>

mod_templates.php:
------------------

Code: Select all

<?php

function strReadTemplate ($strTemplateName, $filetype="htm") &#123;

$strTemplateFile = "../templates/".$strTemplateName.".template.".$filetype;
    
$strTemplateContent = implode("", file($strTemplateFile));
    
return $strTemplateContent;
&#125;

?>
loeschen.template.html
----------------------

Code: Select all

<html>
<head>
<title>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../common/style.css" type="text/css">
<style type="text/css">

</style></head>
<body>
Hier steht folgendes: <?=$strMeldung?>
</body>
</html>

OUTPUT
*******
WRONG outputed source code on server1, the place holder (variable) is as is!
------------------------------------------------

Code: Select all

<html>
<head>
<title>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../common/style.css" type="text/css">
<style type="text/css">

</style></head>
<body>
Hier steht folgendes: <?=$strMeldung?>
</body>
</html>
CORRECT outputed source code on server2:
Works fine, the variable $strMeldung is evaluated and outputs "foobar"
-----------------------------------------------------------------------------

Code: Select all

<html>
<head>
<title>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../common/style.css" type="text/css">
<style type="text/css">

</style></head>
<body>
Hier steht folgendes: foobar
</body>
</html>
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

[Solved] Problems with eval()

Post by visionmaster »

I found the solution. The parsing of short-tags was deactivated on Server2, I set "short_open_tag" in php.ini to "on", restarted my daemon and it works out fine now!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i think you're better off if you disable it again, and start using <?php instead of <?.

because, you will run into problems if you
fe a page that starts like this: <?xml .....
Post Reply