0

I have this code which getting html code of page and than replace all the HREF attributes of the A' tag to redirect it to my site , than my site load the page and again redirect the links and so on...

<?php

libxml_use_internal_errors(true);             // hide the parsing errors
$dom = new DOMDocument;                      // init new DOMDocument
if($_GET){
    $dom->loadHtmlFile($_GET['open']); // getting link to redirect to
}else{
    $dom->loadHtmlFile('http://www.stackoverflow.com');  // getting default site
}
$dom->loadHtmlFile('http://www.stackoverflow.com');      // load HTML into it
$xpath = new DOMXPath($dom);                             // create a new XPath
$nodes = $xpath->query('//a[@href]');                    // Find all A elements with a href attribute
foreach($nodes as $node) {                               // Iterate over found elements
    $node->setAttribute('href', 'index.php?open=http://www.stackoverflow.com'.$node->getAttribute('href'));  // Change href attribute
}
echo $dom->saveXml();                                    // output cleaned HTML

?>

the code is perfectly running , the only problem is that it won't load CSS files somehow.. you're more than welcome to test this code and see what's the problems!

here is online version: http://browser.breet.co.il

thank you in advance!

Community
  • 1
  • 1
homerun
  • 19,837
  • 15
  • 45
  • 70
  • You need to replace the `href` attributes of all the `` elements as well... along with many other things. Have your considered the `action` of forms? Have considered the `src` of scripts? Alternatively, maybe you could just put a `` element in the head to fetch incidental resources from the source site. – DaveRandom Feb 09 '12 at 23:21

1 Answers1

1

Use saveHTML() instead of saveXml()

Using the last one, there's an xml definition at the start of the printed code so it doesn't parse correctly.

Eduardo Reveles
  • 2,155
  • 17
  • 14