Transform code to eval ()

0

I wanted to convert this code to eval (), because it is only reading the files as html from the data.php and the php scripts do not.

// arquivo cujo conteúdo será enviado ao cliente
$dataFileName = 'data.php';

while ( true )
{
    $requestedTimestamp = isset ( $_GET [ 'timestamp' ] ) ? (int)$_GET [ 'timestamp' ] : null;
    // o PHP faz cache de operações "stat" do filesystem. Por isso, devemos limpar esse cache
    clearstatcache();
    $modifiedAt = filemtime( $dataFileName );

    if ( $requestedTimestamp == null || $modifiedAt > $requestedTimestamp )
    {
        $data = file_get_contents( $dataFileName );

        $arrData = array(
            'content' => $data,
            'timestamp' => $modifiedAt
        );

        $json = json_encode( $arrData );
        echo $json;
        break;
    }
    else
    {
        sleep( 2 );
        continue;
    }
}
    
asked by anonymous 25.03.2014 / 16:56

2 answers

2

I think this might help:

ob_start(); // Ativa o buffer de saída
include($dataFileName); // Inclui o arquivo dentro do buffer
$data = ob_get_contents(); // Copia o buffer para a variável $data
ob_end_clean(); // descarta o conteúdo do buffer sem fazer nenhuma alteração na página

You basically run the include contents and send them to a variable without it interfering with the current script.

    
25.03.2014 / 19:48
1

If you want data.php to be processed by the server pass it as a url, to file_get_contens () .

$data = file_get_contents('http://localhost/projeto/template.php');

I made an example trying to simulate your error, create two files processa.php and template.php

template.php

<h1>template</h1>
<?php
     $i=0;
    while($i < 11){
        echo $i .'<br>';
        $i++;
    }
?>

process.php

When executed this way the output was the html and php code printed on the screen, ie did not display the values of $i .

echo file_get_contents('template.php');

When placed as a url it printed the values of $i

echo file_get_contents('http://localhost/projeto/template.php');
    
25.03.2014 / 18:25