Web Hosting Talk







View Full Version : Printer friendly script problem...


theqase
07-18-2005, 08:51 AM
I keep getting this nasty error on all of my print scripts that I try on my server.

Warning: file(http://www.domain.com/public_html/middle/supply.html) [function.file]: failed to open stream: Connection refused in /Applications/xampp/htdocs/public_html/print.php on line 104

Warning: Invalid argument supplied for foreach() in /Applications/xampp/htdocs/public_html/print.php on line 105


here is the script I am using....

<?
header("Expires: Mon, 26 Jul 1997 01:00:00 GMT");
header("Last-Modified: ". gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");

/*
|| @(#)print.php v1.02 20 May 2002
||
|| Name: print.php
|| Version: 1.02
|| Author: Torrent
|| WWW: http://www.ski-info-online.com
||
|| Change Log:
|| Version Date By Description
|| 1.01 29Mar02 Torrent First public release
|| 1.02 20Apr02 Torrent Use of Super Globals (4.1.x)
||
|| =============== Conditions of Use =================
|| This file is free for distribution, modification
|| and reuse. It comes with no warranty expressed or
|| implied. Also you use it at your own risk and I'm
|| completely and utterly blameless for anything that
|| arises from this use :)
||
|| As with all scripts that get written you always
|| think of a hundred improvements and "cool" enhance-
|| -ments during its development. As always though you
|| rarely have the time to implement them all and test
|| them. If you make any modifications that you feel
|| others can benefit from then please release it back
|| to the community. Under no circumstances can any
|| financial or monetary gain be made from any part of
|| this script.
|| ===================================================
||
|| ==================== Purpose ======================
|| This script allows you to make a printable version
|| of any web page. Its strength lies in the fact that
|| it does this for both static and/or dynamically
|| produced pages through the use of HTTP GET method,
|| without writing a file to the server.
|| ===================================================
||
||
|| Installation instructions:
|| 1. Copy the file to a folder on your server.
|| 2. For your other web pages make a link to print.php
|| e.g. <a href="print.php">Print Me</a>
|| 3. Add into your web pages the "start" and "end"
|| comments (<!-- START --> & <!-- END CONTENT -->
|| 4. Test it out by clicking on the links in those pages
||
|| ====================================================
*/

if ($_SERVER['HTTP_REFERER']){
$url = &$_SERVER['HTTP_REFERER'];
} else {
// Default page to go to if the script was not invoked
// through an HTTP referral (i.e. they didn't follow a
// link). Change this to a default page of your choosing.
$url = "http://www.domain.com/";
}

// These are the tags that determine where the printable
// content starts and where it should end. Remember to add
// them to your web pages. :)

$START_CONT="<!-- CONTENT STARTS -->";
$END_CONT = "<!-- CONTENT ENDS -->";

$content=0;

$parsed_url = parse_url($url);
$myServer = $parsed_url['host'];
$document = $parsed_url['path'];

// replaces any space in the query with +
$url = preg_replace('/[[:space:]]/','+',$url);

if($document[strlen($document)-1]=='/'){
// If your home page has a name other than
// index.php then change this here.
$document = "$document/index.php";
$base_url = dirname($document);
}
?>

<HTML>
<HEAD>
<BASE HREF="<? echo "http://$myServer$base_url/"; ?>">
<!-- NOTE! I recommend you set up a stylesheet which is specific -->
<!-- for the printed page. If you do not want to do this then -->
<!-- comment the next line out. Otherwise replace the printable.css -->
<!-- with the name of your style sheet -->
<link rel="stylesheet" href="printable.css">

<?
// --- DO NOT MODIFY ---
$f_contents = file($url);
foreach($f_contents as $line){
if(ereg($START_CONT,$line)){
$content=1;
}
if(ereg($END_CONT,$line)){
$content=0;
}
if($content==1){
echo $line;
}
}
?>
<!-- Place your Copyright message here -->
<HR>
<CENTER>
<SMALL>Copyright &copy; <U>Ski-Info-Online.com</U>. All rights reserved.</SMALL><BR>
<?echo "<A HREF=\"$url\"><SMALL><I>$myURL</I></SMALL></A>\n";?>
</CENTER>
</BODY>
</HTML>






Any ideas why this wont work? I have a feeling there is a permission problem on the server But i dont know...

ilyash
07-18-2005, 11:53 AM
looks like this belongs in the programming forum


is $f_contents an array_expression?

Thats what the error seems to be :

Invalid argument supplied for foreach() in /Applications/xampp/htdocs/public_html/print.php on line 105

Dan L
07-18-2005, 01:03 PM
Edit: Try

foreach($f_contents as $line_num => $line){

instead of

foreach($f_contents as $line){


Also, it looks like it can't find the file in the first place, which is what causes those errors (though I'll bet the above fix would be needed, since the previous example would only be grabbing line numbers.)