php - PHP 警告:extract() 期望参数 1 是数组
问题描述
在错误日志中我收到此错误PHP Warning: extract() expects parameter 1 to be array
,这将使服务器超载并增加 CPU 使用率。我的 php 函数有什么问题。
<?php
function rel2abs($rel, $base) {
if (empty($rel)) $rel = ".";
if (parse_url($rel, PHP_URL_SCHEME) != "" || strpos($rel, "//") === 0) return $rel;
if ($rel[0] == "#" || $rel[0] == "?") return $base.$rel;
extract(parse_url($base));
$path = isset($path) ? preg_replace('#/[^/]*$#', "", $path) : "/";
if ($rel[0] == '/') $path = "";
$port = isset($port) && $port != 80 ? ":" . $port : "";
$auth = "";
if (isset($user)) {
$auth = $user;
if (isset($pass)) {
$auth .= ":" . $pass;
}
$auth .= "@";
}
$abs = "$auth$host$path$port/$rel"; //Dirty absolute URL
for ($n = 1; $n > 0; $abs = preg_replace(array("#(/\.?/)#", "#/(?!\.\.)[^/]+/\.\./#"), "/", $abs, -1, $n)) {}
return $scheme . "://" . $abs;
}
解决方案
正如上面评论中提到的,parse_url()
如果函数 fan 无法解析 URL,则返回 false。并且extract()
需要一个数组作为第一个参数(不是布尔值)。
所以而不是:
extract(parse_url($base));
你需要这样的东西:
$parsedUrl = parse_url($base);
if ($parsedUrl === false) {
throw new \RuntimeException(sprintf('Unable to parse url [%s]', $base));
}
extract($parsedUrl);
推荐阅读
- firebase - 如何翻译 Firebase 推送通知?
- windows - 将 Windows 服务项目更新到 .NET 5 System.Configuration.Installer,如何将其添加到我的项目中?
- java - Java:客户端不从服务器读取流
- haskell - 如何使用haskell中的any函数?
- imagemagick - 将 ImageMagick 链接在一起以将背景设置为白色并将文件拆分为正方形
- mysql - 如何构建数据库以避免表中的重复
- visual-studio-code - VSCode 内置扩展“typescript-language-features”已经启动了一个 tsserver,有没有办法通过我的扩展与之交互?
- c++ - 为什么我不能在 C++ 的主函数中定义函数?
- flutter - 在颤振中进行集成测试时如何获取设备ID?
- amazon-web-services - 捕获和解析 AWS RDSDataService 异常