PHP: Equivalent of include using eval

后端 未结 8 1257
清酒与你
清酒与你 2020-12-06 05:56

If the code is the same, there appears to be a difference between:

include \'external.php\';

and

eval(\'?>\' . file_get_conten

8条回答
  •  無奈伤痛
    2020-12-06 06:09

    here is my approach.

    it creates temporary php file and includes it.

    but this way if code you want to run on this function has errors program exits before removing temporary file

    so i make an autoclean procedure in function. this way it cleans old temporary files by an timeout everytime function runs. you can set timeout or disable it from options at start of function

    i also added ignore error option for solving non removed temporary files. if errors ignored, program will continue and remove temporary file.

    also some projects have to disable autoclean because it scans whole directory everytime it runs. it could hurt disk performance.

    function eval2($c) {
        $auto_clean_old_temporary_files=false; //checks old temporary eval2 files for this spesific temporary file names generated by settings below
        $ignore_all_errors=true; //if you ignore errors you can remove temporary files even there is an error 
    
        $tempfiledirectory=''; //temporary file directory
        $tempfileheader='eval2_'; // temporary file header 
        $tempfiletimeseperator='__'; // temporary file seperator for time
        $tempfileremovetimeout=200; // temp file cleaning time in seconds
    
        if ($auto_clean_old_temporary_files===true) {
    
            $sd=scandir('.'); //scaning for old temporary files 
            foreach ($sd as $sf) {
                if (strlen($sf)>(32+strlen($tempfileheader)+strlen($tempfiletimeseperator)+3)) { // if filename long enough
                    $t1=substr($sf,(32+strlen($tempfileheader)),strlen($tempfiletimeseperator)); //searching time seperator
                    $t2=substr($sf,0,strlen($tempfileheader)); //searching file header
    
                    if ($t1==$tempfiletimeseperator && $t2==$tempfileheader) { //checking for timeseperator and file name header 
                        $ef=explode('.',$sf); 
                        unset($ef[count($ef)]);//removing file extension 
                        $nsf=implode('.',$ef);//joining file name without extension
    
                        $ef=explode($tempfiletimeseperator,$nsf);
                        $tm=(int)end($ef); //getting time from filename
    
                        $tmf=time()-$tm;
                        if ($tmf>$tempfileremovetimeout && $tmf<123456 && $tmf>0) { // if time passed more then timeout and difference with real time is logical 
                            unlink($sf); // finally removing temporary file
                        }
                    }
                }
            }
        }
    
        $n=$tempfiledirectory.$tempfileheader . md5(microtime().rand(0,5000)). $tempfiletimeseperator . time() .'.php'; //creating spesific temporary file name
        $c='

提交回复
热议问题