You need to enable JavaScript to run this app. 如何在PHP的shell_exec调用中向Discord bash脚本正确传递GET变量
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP的shell_exec调用中向Discord bash脚本正确传递GET变量

问题修复说明

问题根源

  1. 原代码中--text参数的内容没有将固定文本和$sale变量拼接为完整整体,变量被识别为shell命令的独立参数,没有传入消息文本中
  2. 直接将未转义的用户输入$_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 07:45:05