【问题标题】:Retrieve the original URL from a shortened link [duplicate]从缩短的链接中检索原始 URL [重复]
【发布时间】:2021-02-25 13:36:28
【问题描述】:

我想在单击缩短链接并访问目的地之前从缩短的链接中检索原始 URL。

我有这个外部链接 https://example.com/api.php?redirect=XXXXXXX 重定向到 https://new.com 。 如何使用 PHP 或 Javascript 获取原始链接 https://new.com

【问题讨论】:

  • 我只想问...https://example.com/api.php?redirect=xxxxx 是如何成为https://new.com 的缩短链接?此外,在提出问题时,您应该指定特定语言,而不是添加替代语言。只需标记您喜欢解决方案的语言。我也看不出 html 与此处的相关性。
  • 你还需要自己做一些研究,先做一些尝试。如果您在代码过程中遇到特定的问题,请回来向我们展示您尝试过的内容、预期结果以及您得到的结果。

标签: javascript php html url redirect


【解决方案1】:

const extractParams = (url) =>{
    const search = url.split("?")[1];
    return Object.fromEntries(
        search.split("&").map(param => param.split("="))
    )
}

window.addEventListener("load", ()=>{
    // select all a tag containg word redirect in href attr
    const links = document.querySelectorAll('a[href*="redirect"]')
    links.forEach(el => {
       el.addEventListener("click", (e) =>{
       /// remove line below in your solution if you need to links works correctly
            e.preventDefault();
            const url = e.target.href;
            const params = extractParams(url);
            //belove you have access to redirect and any otter url params
            console.log(params.redirect);
       })
    })
})
<a href="https://example.com/api.php?redirect=https://new.com">New</a>
<a href="https://example.com/api.php?redirect=https://ben.com">Ben</a>
<a href="https://example.com/api.php?redirect=https://den.com">Den</a>

【讨论】:

    【解决方案2】:

    使用$_SERVER['HTTP_REFERER']

    它返回将用户代理引向当前页面的页面地址(如果有)。

    更多详情请参考:https://www.w3schools.com/php/php_superglobals_server.asp

    【讨论】:

    • 不确定这将如何以任何方式回答他们的问题?问题是:“我想在单击缩短链接并访问目的地之前从缩短的链接中检索原始 URL。” 这只会为他们提供请求来自的 URL。但并非总是如此,因为已知 HTTP_REFERER 并不总是被设置并且不能被信任,因为客户端可以覆盖它。
    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多