Nasıl çoklu seviyelerde () aracılığıyla bir PHP kodu eval mi?

1 Cevap php

Bu kodu vardır:

$layout_template = template_get("Layout");
$output_template = template_get("Homepage");
$box = box("Test","Test","Test");
eval("\$output = \"$layout_template\";");
echo $output;

In the $template_layout variable is a call for the variable $output_template, so then the script moves onto the $output_template variable

Ama $ output_template içinde, herhangi bir daha gitmez değişken $ kutusuna bir çağrı olduğunu, ama bir seviyede herhangi bir daha fazla gitmez

1 Cevap

I never iç içe eval() istiyorum, ve özellikle hiçbir recursive mantık olacaktır. Kötü haber. PHP's Include yerine kullanın. IIRC eval() ise havai, yeni bir yürütme içeriği oluşturur include() yok.

: Sen gibi tamponlar varsa

<h1><?php echo $myCMS['title']; ?></h1>

Ben bazen Index.tpl şekilde erişimi böyle bir ilişkisel dizi yukarıdaki gibi, o zaman sadece sınıfta yaptığı gibi dosyaları var:

<?php
   class TemplateEngine {
       ...
       public function setvar($name, $val)
       {
            $this->varTable[$name]=make_safe($val);
       }

       ....
       /* Get contents of file through include() into a variable */
       public function render( $moreVars )
       {
           flush();
           ob_start();
           include('file.php');
           $contents = ob_get_clean();
           /* $contents contains an eval()-like processed string */
           ...

Ödeme ob_start() and other output buffer controls

Eğer kötü kod girişleri sterilize hakkında süper güvenli, eval() veya user data inclusion her türlü kullanabilirsiniz yaparsanız.

Eğer bir tür kombine widget / şablon sistemi yazıyoruz gibi görünüyor. Sınıflar olarak widget (views) yazın ve onları to be used in existing şablon sistemleri sağlar. $myWidget->render($model) ve böylece jenerik şeyler tutun.

Ben PHP doc-user-comments zımbırtısının bu gördüm ve bunun kötü bir fikir gibi görünüyor:

<?php
$var = 'dynamic content';
echo eval('?>' . file_get_contents('template.phtml') . '<?');
?>

Belki birisi bu konuda bana aydınlatmak: P