简体   繁体   English

PHP将域重定向到正确的域

[英]PHP redirect domain to the correct domain

I have two websites (domain1.com and domain2.com) with link button to basedomain.com website. 我有两个网站(domain1.com和domain2.com),带有指向basedomain.com网站的链接按钮。 In the basedomain.com I have one button to link back to the domain1.com 在basedomain.com中,我有一个按钮可以链接回domain1.com

How I can redirect the users back to the website from which they came (domain1.com or domain2.com) 我如何将用户重定向回他们来自的网站(domain1.com或domain2.com)

in basedomain.com I have a simple a href link: 在basedomain.com中,我有一个简单的href链接:

<a href="http://domain1.com/" >Go back to Homepage</a>

Add the link of the 'from' domain to your basedomain link like this: 像这样将'from'域的链接添加到您的basedomain链接中:

<a href="http://basedomain.com?from=domain1.com">

Optional you can add some security to your link to verify that the 'from' part isn't altered, using HMAC. 可选,您可以使用HMAC在链接中添加一些安全性,以验证“发件人”部分未更改。 Or match the 'from' value at you base domain against a whitelist that contain the possible 'from' URL's. 或者将您基础域中的“发件人”值与包含可能的“发件人” URL的白名单相匹配。

Hope this helps? 希望这可以帮助?

EDIT 编辑

Easiest way for now is to go with the whitelist variant. 目前最简单的方法是使用白名单变体。 To do that, you have to do the following steps: 为此,您必须执行以下步骤:

  1. Create a link to the basedomain from each (sub)domain like i've done above. 像我上面所做的那样,从每个(子)域创建到basedomain的链接。
  2. The 'from' var contains the identifier of the current domain, for example the name (domain1.com) “ from”变量包含当前域的标识符,例如名称(domain1.com)
  3. Build an array on your basedomain containing the valid enrties like this: 在您的basedomain上构建一个包含有效实体的数组,如下所示:

    \n$validDomains = [ $ validDomains = [\n    'domain1.com' => 'http://www.domain1.com/', 'domain1.com'=>'http://www.domain1.com/',\n    'domain2.com' => 'http://www.domain2.com/', 'domain2.com'=>'http://www.domain2.com/',\n    .... ....\n]; ];\n
  4. Building the back link on your base domain, start with checking the 'form' key against the validDomains. 在您的基本域上构建反向链接,首先要根据validDomains检查“ form”键。 If the key exists, add the value of the validDomains as href link: 如果键存在,则将有效域的值添加为href链接:

    \n$linkForYourHref = null; $ linkForYourHref = null;\nif (!empty($_GET['form']) && isset($validDomains[$_GET['form'])) { if(!empty($ _ GET ['form'])&& isset($ validDomains [$ _ GET ['form'])){\n    $linkForYourHref = $validDomains[$_GET['form']; $ linkForYourHref = $ validDomains [$ _ GET ['form'];\n} }\n\n\n//button code //按钮代码\nif (!is_null($linkForYourHref)) { 如果(!is_null($ linkForYourHref)){\n     //link to $linkForYourHref //链接到$ linkForYourHref\n} }\n

An another options is to authenticate your 'from' part of the basedomain link by adding a hash to the URL like this: 另一个选择是通过向URL添加哈希来对basedomain链接的“ from”部分进行身份验证,如下所示:

<a href="http://basedomain.com?from=domain1.com&key=theHMACkey">

See Paragonie for more info about that 有关更多信息,请参见Paragonie

You can use 您可以使用

HTTP_REFERER - which holds the previous url from which the user came from HTTP_REFERER-保留用户来自的先前URL

  • store the url in a variable and output it inside href 将网址存储在变量中,并将其输出到href中

      <?php if($_SERVER['HTTP_REFERER']!=""){ $link = $_SERVER['HTTP_REFERER']; } else { $link = "http://www.default_link.com"; } echo '<a href="'.$link.'" > Go back </a>'; ?> 

您可以使用

header("Location: http://basedomain.com");

Use 采用

 $refer = $_SERVER['HTTP_REFERER'];

to get the domain. 获得域名。

And use 并使用

 header('Location: '.$refer);

to redirect. 重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM