Infinite loop with data stream in PHP

1

I have the following code below:

<?php
$Start = microtime(true);

$Code = <<<'Code'
<?php
for($i = 0; $i < 300; $i++){
    echo '.';
}
Code;

include 'data://text/plain,'.$Code;

echo microtime(true) - $Start;

PHP is entering an infinite loop in which it was not meant to enter. It looks like the stream can not store the value of the $i variable. How do I resolve this issue?

    
asked by anonymous 19.10.2015 / 23:21

1 answer

2

By testing the problem is in the + character

It is deleted in the final code because it is not properly escaped.

A code that works here for you is this:

$Start = microtime(true);

ini_set('max_execution_time', 5);

$Code = <<<'Code'
<?php
    for($i=0; $i < 300; $i++) { 
    echo $i . "\n";
};
?>
Code;


include_once 'data://text/plain,' . urlencode($Code);

echo microtime(true) - $Start;
?>

And here using what you mentioned about php: // memory

<?php
$Start = microtime(true);

ini_set('max_execution_time', 5);
$Code = <<<'Code'
<?php
    for($i=0; $i < 300; $i++) { 
    echo $i . "\n";
};
?>
Code;

 $fp = fopen('php://memory', 'rw'); 

fwrite($fp, urlencode($Code)); // escapando corretamente os caracteres 

fseek($fp, 0); // Retornando o ponteiro ao inicio do bloco de memória
include_once 'data://text/plain,' . stream_get_contents($fp); // incluindo como um arquivo

echo microtime(true) - $Start;
?>
    
20.10.2015 / 00:17