views:

993

answers:

4

Hello there everybody! I've run into a problem lately when coding in PHP and file_get_contents. My problem is that when i load a website like this:

<? echo file_get_contents($_GET['url']); ?>

The pictures of the website i load doesn't show. For example when I go to Google, no pictures are shown. This is for every website i visit. How can i fix this?

+9  A: 

The HTML page you are displaying assumes you also have the images available, which you don't as they are on the original page's server (e.g. Google.com).

The quickest way to make sure everything on the HTML page loads is to add <base href="http://www.google.com/" />. This tells the browser to go back to the original path for the rest of the contents including images, CSS, scripts etc.

You'll want to inject that between the <head></head> of the HTML page you're displaying. You could use a regular expression or Simple HTML DOM.

Hope that helps

Al
Sorry, but I'm a total noob on this. Where should i put the <base href="" /> code? Inside the php code i have?
Nope, add it inside the HTML you are echoing. Put the result of file_get_contents($_GET['url']) into a string, then you can change the HTML before you echo it.
Al
I did like this, but hmmm. Now the website is shown twice. Sorry for my lack of understanding. It must be irritating.echo $page;echo "<base href=\"".$page."\">";
<? print str_replace("<html>", "<html>\n<base href=\"".dirname($_GET['url'])."\">", file_get_contents($_GET['url'])); ?>
danamlund
+2  A: 

Don't do this. You're stealing other web sites' content. It also doesn't work well, as you've noticed, since all relative URLs are broken.

Can you use an iframe instead? As in:

<iframe src="<?php echo htmlspecialchars($_GET['url']) ?>"></iframe>

This is nicer since you're not hiding the web site you're proxying from the end user.

John Kugelman
isnt using an iframe double stealing ? not only the content but also the traffic ? :-)
Rufinus
You steal the traffic by using file_get_contents. What's the difference?
Mez
A: 

I think this is because the image urls are relative <img src="/img/foo.png"> meaning it looks for the image on your server instead and say googles. Fixing this requires looking through all urls in the source and changing them from relative to absolute.

danamlund
A: 

file_get_contents() does what it says, gets the content of the file or Url supplied as argument. An HTML page doesn't have images inside it, they're not the page's content, the HTML page only has references to external files, that have their own content.

Petruza
yeye noob, if you are going to answer the question then answer, if not then get the hella out
Ok you don't have to troll like a little kid.
Petruza
Since you couldn't help then get out. I solved it myself. Noob.
Will, the up and downvote buttons are provided as a way to indicate the helpfulness of the answer. Name-calling comments are not the right way.
bmb
Petruza, your answer is technically correct. However, in this case, the results of file_get_contents() is displayed back to the user's browser with the echo command. Presumably, the browser is then making the subsequent calls to retrieve the images. Will's problem is that the URLs of the images point to the wrong locations -- likely because they are relative as others have pointed out.
bmb