简体   繁体   中英

PHP Eval String - Alternative?

In a text file (eg. file1.txt), let's say I have a line of php:

myArray['index'] = "awesome"

In my PHP file (eg. file2.php) I have this line of text imported and assigned to $string.

Is there a way to evaluate the contents of $string without using the eval function?

I currently do:

eval("{$string};");

To clarify the reason I am asking: I've read that using eval is "evil". For this reason I want to see if there is an alternative method of evaluating php code that is stored in a text file. I know it might seem weird. You may be thinking "why don't you just write the code in the php file?", but the reason I'm doing this is not really my question. I'm just wondering about alternatives to using eval. Thanks.

Put a <?php in the top of the file and then include() it. But if any of the file's contents come from untrusted users, you have a security vulnerability on your hands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM