...

View Full Version : Mimick "View Source" with PHP on Dynamic Page



jakerbug
01-08-2006, 07:16 PM
I want to be able to output the exact source code that I see when I click "View Source" directly onto the page I'm looking at. I've been trying for DAYS and I can't get it. Is it possible?

The goal is to have a user review the dynamically generated page they are looking at and then use "fopen" to take the source code from that page and create a static "index.html" file. I just can't figure out any good way with PHP, JavaScript (innerHTML???), or even XML (httprequest), or any combination of those.

I'm thinking if I can get that code exactly how it appears in the "View Source" or an "httprequest" into a text box on my page somehow, I'd be set to then pull the text from that text box and create the file with PHP. Am I that far off?

It seems to me like this should be relatively easy, but I'm so far off in the wrong direction that I don't even know where I went wrong.

GJay
01-08-2006, 07:31 PM
If you buffered the output (ob_start() at the top of the page), then you could put the output into a text-area, or html_entities() it, instead of displaying it....


ob_start();

//The page as it is now


if($_GET['display']=='source') {
$output=ob_get_contents();
ob_end_clean();
echo '<html><body><textarea>'.$output.'</textarea></body></html>';
//or
echo html_entities($output);
}
else {
ob_flush();
}

jakerbug
01-08-2006, 07:59 PM
I placed this code in the page:


<?php
ob_start();


echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>

<html>
<head>
<title>Personalized Template Selection for $company</title>
</head>

<body bgcolor='#9AA3B0'>

*****THE REST OF THE PAGE*****

</body></html>";

if($_GET['display']=='source') {
$output=ob_get_contents();
ob_end_clean();
echo '<textarea>'.$output.'</textarea>';
}
else {
die( "Error");
}
?>

But I get my error message every time i try it. Can you see why?

Element
01-08-2006, 08:06 PM
<?php

ob_start(); // This should be at the top of the document before any HTML.

?>

<?php

$page_source = ob_get_contents(); // The contents of the page.

$source_output = "<textarea cols=\"400\" rows=\"20\">:source:</textarea>";

echo str_replace(":source:", $page_source, $source_output); // Had to use this because of the characters in the page source, not all pages need it this way but I had to for a page with alot of Javascript and such.

?>

<?php

ob_end_flush(); // this should be at the bottom of the document, under all the HTML.

?>


Thats what I use. Works perfectly.

jakerbug
01-08-2006, 08:17 PM
Works perfectly Element. Thanks. I think that's basically what GJay was getting at too, but I was just a little too dumb to implement it... :o

Hopefully I can figure it out from here...

Element
01-08-2006, 08:28 PM
The ob functions they were using didn't work for me, I had to simply use ob_end_flush() at the end when I was doing it.

Kurashu
01-09-2006, 12:17 AM
Or, instead of going through and using the buffer functions you can do this:


highlight_file(__FILE__);

http://us3.php.net/highlight_file

trib4lmaniac
01-09-2006, 07:44 PM
Or, instead of going through and using the buffer functions you can do this:


highlight_file(__FILE__);
http://us3.php.net/highlight_file
That woud print the PHP source.

marek_mar
01-09-2006, 08:04 PM
How about adding a "Content-Type: text/plain" header?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum