user6603766 user6603766 - 4 months ago 14
Javascript Question

PHP System call with JS variable

This is my Javascript:

var url = "load.php?x="+x;
xmlhttp.open("GET", url, true);


where x is a defined variable.

This is my PHP:

$var = $_GET['x'];
echo shell_exec('./test2/test2 $var');


Where test2 is a folder with an executable file and $var is a parameter. This works when my second statement is

echo shell_exec('./test2/test2 param');


This shows up in my error log:

terminate called after throwing an instance of 'terminate called after throwing an instance of 'std::logic_errorstd::logic_error'
what(): what(): basic_string::_S_construct NULL not validbasic_string::_S_construct NULL not valid
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid


What am I doing wrong?

Thank you!

Answer

I think your issue is because you're trying to have a variable interpolated in a string that is defined using single quotes. Use double quotes instead. Relevant quote from the manual:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

echo shell_exec("./test2/test2 $var");
Comments