PHP 传出/外部链接

发布于 2024-10-01 00:48:26 字数 506 浏览 2 评论 0原文

您好,如果有人点击外部链接,我希望实现与 deviantART 上看到的类似功能:示例: http://www.deviantart.com/users/outgoing?http://driz.co.uk/

实现这样的最佳方式是什么特征?从 dA 上的情况来看,他们只需编辑所有外部链接以添加 http://www.deviantart.com/users/outgoing? 前缀,然后显示允许他们访问的页面网站或返回。因此,也许我需要某种方法来检查哪些链接不在当前域上,然后在它们前面加上一个特殊的 url 前缀以显示垫脚石页面。

或者可能只是将 rel="external" 附加到链接,然后以某种方式为所有具有 external rel 标记的链接转到类似的页面,或者可能显示一个显示相同消息的对话框。

干杯

Hi I'm looking to implement a similar feature to that seen on deviantART if a person clicks onto an external link: example: http://www.deviantart.com/users/outgoing?http://driz.co.uk/

What would be the best way of implementing such a feature? From the looks of it on dA they just edit all the external links to have the http://www.deviantart.com/users/outgoing? prefix and then show that page which allows them to visit the site or return. So perhaps I'd need someway of implementing a way to check which links are NOT on the current domain and then prefix them with a special url to show a stepping stone page.

Or perhaps just appending the rel="external" to the links and then somehow for all links that have the rel tag of external go to page like that or perhaps show a dialog showing the same message.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生死何惧 2024-10-08 00:48:27

对于用户输入 html,使用正则表达式:

<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]

其中 $1 将是 href,现在您可以测试:

(\w:\/{2})?([\w\.]+)

如果 $2 不是您的域之一,您需要重写 href,因此在第一个正则表达式中找到 $1 的位置(strpos在 php 中)并添加掩码 url

http://yourdomain.com/maskurl?

查询字符串将是您想要重定向到的 url

在另一侧,如果您要打印具有给定 url 的链接,则只需传递第二个。

这里的第一个正则表达式: http://www.onaje.net /content/working-regular-expressions-href-url-extractor 另一个是我写的,但没有测试过。

For the user input html use a regular expression:

<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]

where $1 will be the href, now you can test against:

(\w:\/{2})?([\w\.]+)

if $2 is not one of your domains you need to rewrite the href, so locate the position of $1 in the first regexp (strpos in php) and add your mask url

http://yourdomain.com/maskurl?

The query string as is will be the url you want to redirect to

By the other side if you are printing the links with a given url you need to pass only the second one.

The first regexp from here: http://www.onaje.net/content/working-regular-expressions-href-url-extractor and the other was written by me, but tested none.

幼儿园老大 2024-10-08 00:48:27

检测部分:如果链接是用户提供的、可点击的链接,则仅需要这种修饰。您已经拥有将用户提供的链接转换为可点击链接的基础设施(否则,您将面临重大安全问题),因此您所需要做的就是将所有这些链接插入装饰器函数中。

装饰部分:有一个函数接受 URL 参数。如果参数的第一部分是“http://your.domain.name”,或者它是相对链接(不以 http:// 等协议开头),则不要理会它。否则:

$url = "http://your.domain.name/outgoing.php?".urlencode($url);

这将确保原始 URL 中任何不受保护的字符都被正确转义。

重定向部分:在 outgoing.php 脚本中,只需查看 array_keys($_GET) 即可找到 URL 并显示相应的页面。

The detection part: a link only needs this kind of decoration if it's an user-provided, clickable link. You already have the infrastructure to turn user-provided links into clickable links (otherwise, you have a major security issue), so all you need to do is plug all those links into a decorator function.

The decoration part: have a function accept an URL argument. If the first part of the argument is "http://your.domain.name", or if it's a relative link (does not start with a protocol like http://), leave it alone. Otherwise:

$url = "http://your.domain.name/outgoing.php?".urlencode($url);

This will ensure that any unprotected characters in the original URL are properly escaped.

The redirection part: in the outgoing.php script simply look inside array_keys($_GET) to find the URL and display the appropriate page.

我们的影子 2024-10-08 00:48:27

最好的方法是更改​​链接,其中新 URL 将是您自己的脚本,该脚本根据查询字符串重定向用户。如果您想保持与 deviantArt 相同的 URL 结构,请使用 $_SERVER['QUERY_STRING'] 获取重定向脚本中的 URL。

The best way would be to change your links where the new URL would be a script of your own that redirects users based on the query string. If you want to keep the same URL structure as deviantArt, use $_SERVER['QUERY_STRING'] to grab the URL in your redirect script.

梦里泪两行 2024-10-08 00:48:27

非常好的 Netcoder 帮助。这是重定向部分的完整 php:

<?php

$redirect = $_SERVER['QUERY_STRING'].'';

?>

<html><head>

<meta name="robots" content="noindex" />

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">  


<title>Your Page title</title>

</head><body>

your Site Content

</body>

<SCRIPT LANGUAGE="JavaScript">

setTimeout("location.href ='<?php echo $redirect ?>'",30);

</script>

</html>

您的网站内容是您想要展示的内容。 30 是时间
重定向前等待的秒数。将其保存为outgoing.php(或
任何东西)

外部链接必须采用以下格式: http://your-nice-domain.com/outgoing.php?http://the-external-domain.com/external-path.any

重要

  1. 从 robots.txt 中机器人输出此 php 文件

  2. 它与 dA 相同,除了:重定向是自动的。

Excellent help Netcoder. This is the full php of the The redirection part:

<?php

$redirect = $_SERVER['QUERY_STRING'].'';

?>

<html><head>

<meta name="robots" content="noindex" />

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">  


<title>Your Page title</title>

</head><body>

your Site Content

</body>

<SCRIPT LANGUAGE="JavaScript">

setTimeout("location.href ='<?php echo $redirect ?>'",30);

</script>

</html>

your Site Content is what you want to show. 30 is the time in
seconds to wait before the redirection. Save it as outgoing.php (or
anything)

External link must be in this format : http://your-nice-domain.com/outgoing.php?http://the-external-domain.com/external-path.any

Important :

  1. Robot out this php file from robots.txt

  2. It will be the same like dA except : the redirection is automatic.

厌倦 2024-10-08 00:48:27

有几种方法可以解决此问题,具体取决于页面的生成方式。如果所有链接都是由 PHP 生成的,那么可以通过将链接的 URL 与站点的 URL 进行比较来轻松完成。如果它们不同,请将您的 passthrough 页面添加到 URL 的开头,类似于 DA 的做法。

我刚刚完成了一个脚本,用于在每个链接旁边添加外部链接图标,类似于维基百科的做法。对于这个特定的网站,我必须处理许多具有静态内容的页面。使用我上面提到的 PHP 方法对此效果不佳。相反,我用 Javascript 实现了一些东西。使用 jQuery,我选择所有以“http://”或“https://”开头的锚标记,然后循环遍历每个锚标记以检查该域是否与我当前的域匹配。如果不同,我会在链接旁边添加图标。您可以执行类似的操作,但需要在锚点的 href 前面加上您的 passthrough 页面。

There are a few ways you could approach this, depending on how the pages are generated. If all the links are being generated by PHP, then it could easily be done by comparing the URL of the link to that of the site. If they are different, prepend your passthough page to the beginning of the URL, similar to how DA did it.

I just completed a script to add external link icons next to each link, similar to how Wikipedia does it. For this particular site, I had to work with many pages that have static content. Using the PHP method I mentioned above, would not work well for this. Instead I implemented something in Javascript. Using jQuery, I select all anchor tags that start with "http://" or "https://", then I cycle through each one to check if the domain matches my current one. If it is different, I add the icon next to the link. You could do similar, but instead prepend the href of the anchor with your passthough page.

贱人配狗天长地久 2024-10-08 00:48:27
<?php
$html = '<a href="http://google.com/somethingelsewhere">Something</a>'
      . '<a href="http://example.com/somethinginmysite">My site!</a>';

echo preg_replace_callback('/<\s*a\s+[^>]*href\s*=\s*["\']?([^"\' >]+)["\' >]/', function($m) {
     // initialise the data variable from your object
     $link = $m[1];

     // early return
     if(strpos($link, 'http://example') === 0) return $m[0];

     $linklen = strlen($m[1]);
     $start = strlen($m[0]) - $linklen - 1;

     $replacement = 'http://example.com/mymask?link=' . urlencode($link);
     return substr_replace($m[0], $replacement, $start, $linklen);
}, $html);
<?php
$html = '<a href="http://google.com/somethingelsewhere">Something</a>'
      . '<a href="http://example.com/somethinginmysite">My site!</a>';

echo preg_replace_callback('/<\s*a\s+[^>]*href\s*=\s*["\']?([^"\' >]+)["\' >]/', function($m) {
     // initialise the data variable from your object
     $link = $m[1];

     // early return
     if(strpos($link, 'http://example') === 0) return $m[0];

     $linklen = strlen($m[1]);
     $start = strlen($m[0]) - $linklen - 1;

     $replacement = 'http://example.com/mymask?link=' . urlencode($link);
     return substr_replace($m[0], $replacement, $start, $linklen);
}, $html);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文