Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2004
    Location
    New Jersey
    Posts
    70
    Thanks
    2
    Thanked 1 Time in 1 Post

    Mimick "View Source" with PHP on Dynamic Page

    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.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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....
    PHP Code:
    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();


  • #3
    New Coder
    Join Date
    Mar 2004
    Location
    New Jersey
    Posts
    70
    Thanks
    2
    Thanked 1 Time in 1 Post
    I placed this code in the page:

    PHP Code:
    <?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?

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?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.
    Last edited by Element; 01-08-2006 at 07:10 PM.

  • #5
    New Coder
    Join Date
    Mar 2004
    Location
    New Jersey
    Posts
    70
    Thanks
    2
    Thanked 1 Time in 1 Post
    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...

    Hopefully I can figure it out from here...

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Or, instead of going through and using the buffer functions you can do this:

    PHP Code:
    highlight_file(__FILE__); 
    http://us3.php.net/highlight_file

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kurashu
    Or, instead of going through and using the buffer functions you can do this:

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

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    How about adding a "Content-Type: text/plain" header?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •