-2

I am making a proxy website for a project, and it works, but i need to change all the links on the page my site loads so they go through my site and not redirect to the page I loaded.

inbrowser.fluffycraft.net is the site im working on. If you load a page and click on a link it redirects to the website, which is bad, does anyone know how i could do this?

Thanks,

Gus.

snek
  • 1,980
  • 1
  • 15
  • 29

2 Answers2

0

This isn't a very trivial problem. Some sites like https://hide.me/en/proxy have done a good job of it. The complexities come from the fact that proxies don't usually just want to redirect links, but forms, resources, flash objects etc.

In general you use an HTML parser and iterate through the DOM look for things that have URL's like anchor tags, form elements, etc. Read the URL and replace it with your URL with the original URL as a query parameter.

<a href="http://example.com"></a>

Gets changed to

<a href="http://foo.bar/proxy?target=http%3A%2F%2Fexample.com"></a>

As others have mentioned, you may use regular expressions, but you are likely to miss things because regular expressions are not sufficient for parsing HTML.

Community
  • 1
  • 1
Samuel
  • 16,923
  • 6
  • 62
  • 75
0

I assume you are using file_get_contents() to grab the entire source of the page you are linking to and simply outputting the result.

What you would do is use preg_match_all() on the result before outputting it to replace anything that looks like a link with your own link.

Edit 1

I agree with the other answer given than regex can be a minefield with stuff like this. parsing the DOM can be very challenging too. An interesting alternative would be to output the source as is but inject some JavaScript/jQuery which would be a far easier way to interact with the DOM. The script could simply modify the href attributes of any anchor tags it finds.

I'll have a bash at writing it later on unless someone beats me to it!

Edit 2

Here you go. I've tested this on a selection of links and it seems to be good to go. Obviously is subject to your visitor having javascript enabled, but it seems to me like the simplest "one size fits all" solution.

<script>
$(function(){
    $("a").each(
        function(){
            oldlink = $(this).attr("href");
            insertpoint = oldlink.indexOf("//") + 2;
            if (insertpoint = 2) {
                insertpoint = 0;
            }
            newlink = oldlink.substr(0,insertpoint) + "www.mywebsite.com/myscript.php?link=" + encodeURIComponent(oldlink.substr(insertpoint));
            $(this).attr("href",newlink);
        }
    );
});
</script>

Don't forget to include jQuery for this to work!

Download it here https://jquery.com/download/

or just include it like this

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
Darren H
  • 910
  • 8
  • 24