Can anyone show an example how you create printer friendly. I using php 4.32

Error message
Warning: fopen(http://localhost/pcmag/1.php) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Access Denied in D:\projects\pcmag\pfv.php on line 31

Warning: fread(): supplied argument is not a valid stream resource in D:\projects\pcmag\pfv.php on line 31

Notice: Undefined variable: code in D:\projects\pcmag\pfv.php on line 47

http://localhost/pcmag/1.php

<a href="pfv.php" target="_blank"><strong>printer friednly version</strong></a>

<!-- CONTENT -->
TESTING TESTING
<!-- /CONTENT -->

<?php

/*
You can do whatever you like with this script.  It's simple, but extremely useful if you're a programming newbiw.

This script is designed to allow someone to print just the content on a page without all the navigation elements.  It looks for two comments tags in your HTML page:

<!-- CONTENT -->     and     <!-- /CONTENT -->

Anything in between these two comment tags will be returned to the browser.

The script will also print the address of the original content page at the bottom of the printer-friendly page.  If you don't want it, remove the line '<br clear="all"><em>'.$HTTP_REFERER.'</em>'



Step 1.  Save this script in a common area on your site.  It does not have to be in your cgi-bin since it is not a cgi script, and you do not have to change its permissions.

Step 2.  Replace the comments below in the script with your information.

Step 3.  Open your HTML page and insert  <!-- CONTENT -->     and     <!-- /CONTENT -->  around your main content.

Step 4.  Outside of the  <!-- CONTENT -->     and     <!-- /CONTENT -->  tags, add a link to this script, like:

<a href="pfv.php">Printer-friendly version</a>

If you want, you can add a small printer-looking icon in your link.  You can find them lots of places online, like Mapquest.

That's it!
*/  
$HTTP_REFERER = $_SERVER["HTTP_REFERER"]; $fd= fread(fopen($HTTP_REFERER, "r"), 100000); if ($fd)
{
$start= strpos($fd, "<!-- CONTENT -->");
$finish= strpos($fd, "<!-- /CONTENT -->");
$length= $finish-$start;
$code=Substr($fd, $start, $length);
} echo '<html> <head> <title>/* enter a generic title for your site, which will appear in the title bar of the browser window */</title> </head> <body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"> '.$code.' <br clear="all"><em>'.$HTTP_REFERER.'</em> </body> </html>';
?>

    I used this and it works:

    <?php
    $code = file_get_contents($_SERVER["HTTP_REFERER"]);
    
    $parts = explode('<!-- CONTENT -->', $code);
    
    if(count($parts) != 2) exit('Content not in suibtable format');
    
    $html = $parts[1];
    
    unset($parts);
    
    $parts = explode('<!-- /CONTENT -->', $html);
    
    if(count($parts) != 2) exit('Content not in suibtable format');
    
    $html = $parts[0];
    
    echo '<html>
    <head>
        <title>/* enter a generic title for your site, which will appear in the title bar of the browser window */</title>
    </head>
    <body>
    '.$html.'
    </body>
    </html>';  
    ?>

    :: You may need to make sure you can use fopen on full URLs with the "http://" protocol listed, and not just the file name, "./index.php" for example.

    Hope that helps.

      how you make it works?

      i still getting error

      Warning: file_get_contents(http://localhost/pcmag/1.php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Access Denied in D:\projects\pcmag\pfv.php on line 2
      Content not in suibtable format

        15 years later

        hannahsanders242

        1. You responded to a 15 year old thread. It's highly unlikely that user will notice your post.
        2. This is a PHP coding forum. I suggest you look for an appropriate printer-related forum for your questions.
          Write a Reply...