我在 WordPress 上有一个自定义插件,可以获取 url 参数值(示例:test.com/?token=hello)并将其发送到 Salesforce 字段。
升级到 PHP8.0 后,我收到有关未定义数组键的警告,因为并非所有 url 参数都出现在 url 中。
PHP Warning: Undefined array key "url_param_token" in notification.php on line 264
PHP Warning: Undefined array key "url_param_leadint" in notification.php on line 266
通知.php:
'00N0d000003CTjk' => $_SESSION['url_param_token'] //line 264
'00N60000002XMs9' => $_SESSION['url_param_leadint'] //line 266
存储.php
if (array_key_exists('token', $_GET)) {
$rmc_token = $_GET['token'];
$_SESSION['url_param_token'] = $rmc_token;
} else {
$_SESSION['url_param_token'] = "";
}
if (array_key_exists('leadint', $_GET)) {
$rmc_leadint = $_GET['leadint'];
$_SESSION['url_param_leadint'] = $rmc_leadint;
} else {
$_SESSION['url_param_leadint'] = "";
}
有没有办法传递空白会话变量,这样警告就不会触发?
我在 else 语句中尝试了以下方法,但都不起作用:
$_SESSION['url_param_leadint'] = null;
$_SESSION['url_param_leadint'] = "";
$rmc_token = "";
$_SESSION['url_param_leadint'] = $rmc_token;
由于错误发生在您的 notification.php 文件中,只需检查会话变量是否设置即可。只需使用空合并运算符即可。
'00N0d000003CTjk' => $_SESSION['url_param_token'] ?? ""
上面的例子和下面的例子是一样的。
if (isset($_SESSION['url_param_token'])) {
$data['00N0d000003CTjk'] = $_SESSION['url_param_token'];
} else {
$data['00N0d000003CTjk'] = "";
}