如何在PHP的shell_exec调用中向Discord bash脚本正确传递GET变量
问题修复说明
问题根源
- 原代码中
--text参数的内容没有将固定文本和$sale变量拼接为完整整体,变量被识别为shell命令的独立参数,没有传入消息文本中 - 直接将未转义的用户输入
$_GET['sale']传入shell_exec,存在特殊字符截断、执行异常,以及高危的命令注入风险
生产环境可用的修复代码
<?php $sale = $_GET['sale']; // 拼接完整的消息文本 $text = "New sale just arrived from " . $sale; // 对所有可变参数做shell转义,避免特殊字符问题和注入风险 $escaped_text = escapeshellarg($text); $webhook_url = escapeshellarg("https://discord.com/api/webhooks/xxx/x123"); $footer = escapeshellarg("Info"); // 拼接完整执行命令 $command = "/bin/bash discord.sh --webhook-url=$webhook_url --text=$escaped_text --footer=$footer --timestamp"; echo shell_exec($command); ?>
临时测试简化写法(仅适合本地测试确认功能,禁止用于生产环境)
<?php $sale = $_GET['sale']; echo shell_exec('/bin/bash discord.sh --webhook-url=https://discord.com/api/webhooks/xxx/x123 --text "New sale just arrived from '.$sale.'" --footer "Info" --timestamp'); ?>
注意:该写法未做参数转义,仅在确认sale参数没有空格、引号等特殊字符时可以临时使用
内容的提问来源于stack exchange,提问作者rootzy
相关产品推荐
相关产品推荐

