从PHP调用Perl脚本并传递变量,同时还使用可变的Perl脚本名称 [英] Calling Perl script from PHP and passing in variables, while also using variablized perl script name
问题描述
我通常按如下方式从PHP调用perl脚本并以这种方式传递变量,并且它可以正常工作,但是现在我正在构建一个可重用的组件,在这里我还希望对所传递的perl脚本名称进行变体化这让我有些头疼,所以我想知道是否有人可以指出一种更好的方法来执行此操作,因为我的方法行不通..谢谢..
I normally call perl scripts from PHP as below and pass in variables this way, and it works fine, however now I am building a component for re-use where I want to also variablize the perl script name that I am passing in and this is giving me some headaches, so I am wondering if anyone can point out a better way to do this as my way isn't working.. thanks..
没有可变的perl文件名的工作方式:
the way that works without variablized perl filename:
$file = "/var/www/other_scripts/perl/apps/perlscript.pl $var1 $var2 $var3 $var4";
ob_start();
passthru($file);
$perlreturn = ob_get_contents();
ob_end_clean();
我尝试对似乎不适合我的perl文件名进行变种,您可以在上面看到它在初始"中甚至包括$ var在内的情况,我觉得很奇怪,但这似乎是它起作用的唯一方法,而且我不确定如何使用可变的perl文件名来复制它:
My attempt to variablize the perl filename that doesn't seem to be working for me, you can see in the above how it is including even the $var(s) in the initial " ", which I find odd but this seems to be the only way that it works and I wasn't sure how to even replicate this with a variablized perl filename:
$perlscript_file = "/var/www/other_scripts/perl/apps/" . $perlscript .".pl";
$file = $perlscript_file . $var1 . $var2 .$var3 . $var4;
ob_start();
passthru($file);
$perlreturn = ob_get_contents();
ob_end_clean();
推荐答案
您的方法无法正常工作,因为您要串联所有没有空格的参数,从而有效地使它们成为一个参数.
Your way is not working because you are concatenating all the parameters without spaces, effectively making them one parameter.
尝试
$perlscript_file = "/var/www/other_scripts/perl/apps/$perlscript.pl $var1 $var2 $var3 $var4";
顺便说一句,如果参数来自外部来源,则必须使用 escapeshellarg()
. $perlscript
也是如此-如果它来自外部甚至用户输入,请执行
By the way, if the parameters are coming from an external source, you MUST sanitize them using escapeshellarg()
. The same goes for $perlscript
- if it comes from an external source or even user input, do a escapeshellcmd()
on it.
这篇关于从PHP调用Perl脚本并传递变量,同时还使用可变的Perl脚本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!