1

I want to show a url's source code in iframe tag. How to do it ? For example, I have this code.

<html>
<head>
    <title></title>
</head>
<body>
    <div>
        <?php $content = @file_get_contents("http://stackoverflow.com"); ?>
    </div>
</body>

How to show $content in iframe ?

nir
  • 83
  • 1
  • 5
  • 11

3 Answers3

5

use this code: EDIT: iframe version..:

save this source as getsource.php

<html>
<head>
    <title></title>
</head>
<body>
    <div>
        <pre>
        <?= htmlentities( @file_get_contents($_GET['url'])); ?>
        </pre>    
</div>
</body>

then use your iframe somewhere on a different page and add src to getsource.php with url variable:

<iframe src="getsource.php?url=http://stackoverflow.com"></iframe>

This might be not safe, but i think the htmlentities will prevent xss attacks.

MilMike
  • 12,571
  • 15
  • 65
  • 82
3

Add this code to the main page:

<html>
<head>
    <title>Source Code</title>
</head>
<body>
<iframe src="phpscript.php">Your browser doesn't support iframes.</iframe>
</body>

Put this in phpscript.php:

<pre>
<?php echo htmlentities( @file_get_contents("http://www.example.com/")); ?>
</pre>
Friend of Kim
  • 850
  • 9
  • 24
0

Then you'll have to have a separate page with an iframe, whose src points to this page.

Though, may I ask why you have you use an iframe? It seems like you can just show it in a scrollable "div".

Steven Luu
  • 1,047
  • 1
  • 7
  • 13
  • i want to show source code of a webpage to its owner when he/she clicks on the link 'see code' with some highlighted code. – nir Jun 30 '12 at 11:42