Adam Halasz
Adam Halasz

Reputation: 58321

Evaluate PHP through AJAX

What I have and want to do

But I get this error when the eval is executed:

Parse error: syntax error, unexpected '"', expecting T_STRING in /Applications/MAMP/htdocs/Apps/editor/includes/exe.php(5) : eval()'d code on line 1

Evaluate.php

if(isset($_GET["code"])){
    $e = $_GET["code"];
    echo eval($e);
}

The value what I try to evaluate is just:

echo "Hello World!";

Then this is looks like in $_GET["code"] as:

echo \"Hello World!\";

Upvotes: 0

Views: 372

Answers (3)

Donald T
Donald T

Reputation: 10667

According to PHP's documentation:

eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally. It is not possible to catch a parse error in eval() using set_error_handler().

So I think there may be a problem when you run echo eval($e).

P.S. It's best practice not to use double quotes in PHP unless a variable is contained within those quotes. For example, use "Hello, $name" and use 'Hello, Bob'.

Upvotes: 1

Linus Kleen
Linus Kleen

Reputation: 34642

I feel terrible answering this. In your PHP settings, magic_quotes_gpc might be enabled which "corrupts" your incoming data by escaping it.

In order to get it working, you might want to add a little more insecurity to your undertaking by disabling magic quotes.

If that doesn't fix it, debug your input by following Silver Light's suggestions.

Upvotes: 1

Silver Light
Silver Light

Reputation: 45932

Obviously you have an error in a string you are tying to evaluate. Try to output it first and see if it has semi columns and things like that.

But you should never (!) evaluate code you get from URL! Never-never, anyone can send "exec('rm -rf /')".

Upvotes: 1

Related Questions