TL;DR
Single quotes in your PHP webshell code can cause it to fail because they interfere with how the shell interprets commands. Using double quotes, escaping single quotes, or using alternative command execution methods will fix this.
Why Single Quotes Cause Problems
PHP uses both single and double quotes for strings. When you pass a command to be executed by the server (e.g., using system(), exec(), or shell_exec()), the shell needs to interpret that command correctly. Single quotes tell PHP to treat everything inside them literally – no variables are expanded and no special characters are interpreted. This is often not what you want when building a webshell.
Solution Steps
- Use Double Quotes Instead
- Escape Single Quotes
- Alternative Command Execution Methods
escapeshellarg(): This function is designed to escape arguments for use in shell commands, making them safer and more reliable.- Using an Array with
exec(): Passing the command as an array toexec()avoids some quoting issues. - Check Your Input Sanitisation
- Debugging Tip: Print the Command
The simplest solution is usually to switch from single quotes to double quotes. Double quotes allow PHP to expand variables and interpret special characters, which is essential for dynamic command execution.
If you absolutely need to include single quotes within your command string, you can escape them using a backslash (). This tells PHP to treat the single quote as a literal character rather than the end of the string.
Sometimes, even escaping doesn’t work perfectly due to shell-specific rules. Consider these alternatives:
Always sanitise any user input before including it in a command string to prevent code injection vulnerabilities. Never trust user-provided data directly.
Before executing the command, print it to the screen to see exactly what’s being passed to the shell. This can help you identify quoting or escaping problems.

