【问题标题】:PHP Redirect Url with input variable and read from file带有输入变量的PHP重定向网址并从文件中读取
【发布时间】: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


    【解决方案1】:

    您可以通过首先读取文件然后搜索给定条目来实现此目的:

    $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/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多