【发布时间】:2022-10-23 17:25:16
【问题描述】:
我正在寻找一种基于 txt 文件中有条目的 url 参数重定向 url 的方法。例子
我有一个 txt 文件,其中包括
链接1=http://example1.com 链接2=http://example2.com ……
我希望当有人试图访问 url http://something.com/redirect.php?link=link1 时重定向到 http://example1.com
【问题讨论】:
标签: php
我正在寻找一种基于 txt 文件中有条目的 url 参数重定向 url 的方法。例子
我有一个 txt 文件,其中包括
链接1=http://example1.com 链接2=http://example2.com ……
我希望当有人试图访问 url http://something.com/redirect.php?link=link1 时重定向到 http://example1.com
【问题讨论】:
标签: php
您可以通过首先读取文件然后搜索给定条目来实现此目的:
$urls = file_get_contents('urls.txt');
$entries = explode(PHP_EOL, $urls);
foreach ($entries as $url) {
$data = explode(";", $url);
if ($data[0] == $_GET["link"]) {
header("Location: " . $data[1]);
exit();
}
}
echo "Link not found";
为此,您的 urls.txt 必须如下所示:
link1;https://example.com/
link2;https://anotherexample.com/
【讨论】: