Posted by & filed under Programming.

Came across an odd error last week. And by odd, I mean that Googling for the error message only turned up one page.

One of my scripts was using eval as a way to semi-protect the code. Yeah, not a great solution. But it’s not possible to install software on all of the users’ servers and PHP doesn’t have a way to compile. Hopefully that’ll come in the future though.

The error was /file.php (#): eval()’d code does not exist. Googling brought up a page that was having the same issue. After much head banging on desk it finally dawned on me to search my code for “does not exist” and that is what finally brought me to the solution.

What happened is that inside the code I was using __FILE__ to get the path to the eval()’d file and passing that path to another function. That function was checking to see if the file existed before including, and it was using the value from the __FILE__ value passed.

The problem is that __FILE__ inside of eval() includes information about the file being encoded, so “/file.php (#) eval()’d code” didn’t exist as a file on the server, and my function threw the not found error.

Took a bit of regex, and the problem was solved. Instead of passing __FILE__ I passed trim(preg_replace(‘/\(.*$/’, ”, __FILE__)) which stripped out the first parenthesis and everything following and then trimmed off any leading or trailing spaces. Problem solved!

One Response to “PHP, __FILE__, and eval()”

Leave a Reply

Your email address will not be published. Required fields are marked *