PHP重定向脚本:确保点击来自本地


PHP redirect script: ensure click comes locally

如何创建一个PHP脚本,将重定向到一个自定义URL时,在URL中添加链接。例如,当用户访问如下:

http://example.com/link.php?r=http://www.google.com

它应该立即重定向到谷歌。

理想情况下,是否有可能确保点击本身来自本地?

到目前为止,我有这个:

$url = "http://example.com";
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
$refDomain = str_ireplace('www.', '', parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST));
if(strcmp($domain, $refDomain) == 0) {
     header("Location:".$_GET['r']);
}

我添加了link.php文件,但它没有重定向。这可能是因为该文件位于WordPress安装的根文件夹中,但我不认为这应该阻止它工作

尝试使用java-script解决重定向问题,

print ("<script>window.location.href=".$_GET['r'].";</script>");

尝试在Location后面添加一个空格,如下所示。我觉得出口不见了。或者也可以用die。

header("Location: ".$_GET['r']);
exit();
//OR
die();

PHP重定向URL

这似乎每次都有效。我错过什么了吗?

<?php
if ( $_SERVER["REQUEST_METHOD"] == 'GET' && isset( $_GET['r'])) {
    $url = "http://example.com";
    $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
    $refDomain = str_ireplace('www.', '', parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST));
    if(strcmp($domain, $refDomain) == 0) {
        header("Location: {$_GET['r']}");
        exit;
    }
}