Reputation: 58321
What I have and want to do
I have an input area
.
I have a JS script what reads the input area
's innerHTML
and encodes it using encodeURICompontent
then sends the value to evaluate.php?code=+value
;
I have an evaluate.php
what GET
's the code
's value from the URL and returns an evaluated value using eval($code)
to the javascript.
And at the end it puts the xmlHttp.responseText
to a div
.
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
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
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
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