简体   繁体   English

在PHP中将Cookie变量与shell_exec一起使用

[英]Using Cookie Variable with shell_exec in PHP

I'm trying to include a user-defined variable (derived from a cookie) in a shell_exec command to name a file. 我试图在shell_exec命令中包含用户定义的变量(从cookie派生)以命名文件。 Unfortunately, when I run the PHP script, the file name is spat out as just ".flv" without the variable string preceding it. 不幸的是,当我运行PHP脚本时,文件名显示为“ .flv”,前面没有变量字符串。 I've tested the cookie and it does indeed work, but it looks like it's not being passed correctly to shall exec. 我已经测试了cookie,它确实可以工作,但是看起来好像没有正确地传递给must exec。 Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。

$shellvar = $_COOKIE["VideoTitle"];
$shellvar = escapeshellarg($shellvar);
shell_exec('/usr/local/bin/ffmpeg -i audio.wav -i videofile.flv $shellvar.flv 2>&1');

You are using single quotes in the PHP, which means it won't parse the $shellvar as a PHP variable. 您在PHP中使用单引号,这意味着它将不会将$ shellvar解析为PHP变量。 Then that is getting passed to the terminal, which tries to use the variable (which doesn't then exist) and it spits back a blank variable. 然后将其传递给终端,终端尝试使用该变量(该变量不存在)并吐出一个空白变量。 So try replacing the single quotes with double quotes in the shell_exec command. 因此,请尝试在shell_exec命令中将单引号替换为双引号。

So the code would look like: 因此,代码如下所示:

$shellvar = $_COOKIE["VideoTitle"];
$shellvar = escapeshellarg($shellvar);
shell_exec("/usr/local/bin/ffmpeg -i audio.wav -i videofile.flv $shellvar.flv 2>&1");

Obligatory Security Warning: 强制性安全警告:
Also, I'd implore you to be careful, escapeshellarg is a good command, but I'd also recommend (on top of that) only allowing letters and numbers with a preg_match (because I'm paranoid). 另外,请恳请您注意,escapeshellarg是一个很好的命令,但我也建议您(最重要的是)只允许使用preg_match的字母和数字(因为我很偏执)。

Something like 就像是

if (preg_replace('/[A-Za-z0-9]*/', '', $shellvar) != "")
    echo "DISALLOWED CHARACTERS";

This way you can be at least decently sure you aren't allowing malformed names to be passed to the terminal. 这样,您至少可以体面地确定您不允许将格式错误的名称传递给终端。 I'd also recommend running the commands as a specific user (who has very little access, not much more then that of using ffmpeg). 我还建议以特定用户(该用户具有很少的访问权限,而不是使用ffmpeg的访问权限)来运行命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM