Page 1 of 1

No include in new page

Posted: Sat Jul 13, 2002 1:02 am
by salto
Level: newbie. On a page with an include I have a link to a printer-friendly version (pfv.php).
The include works fine on the first page, but nothing to be seen in the new printer-friendly page. I tried the file to be included with a *.php extension, nope.
The include statement *is* (view source) in the new page, the new page has a *.php extension of course, but the file is not included. No error messages. Any help welcome.
Thank you for your time on this matter.

Posted: Sat Jul 13, 2002 1:53 am
by gnu2php
By include, do you mean the include() function?

It would also be helpful you could provide some sample code (the more concise the code, the better).

Posted: Sat Jul 13, 2002 2:15 am
by salto
Thank you, I am always reluctant to take too much bandwith.
The inlcude is on the page is

Code: Select all

<?php include 'include/foo.inc'; ?>
That works fine, foo.inc is included.
The pfv.php link opens a printerfriendly version of the page with

Code: Select all

<?php
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000); 

if ($fd)  
        &#123;  
    $start= strpos($fd, "<!--tekst-->");  
    $finish= strpos($fd, "<!--/tekst-->");  
    $length= $finish-$start;  
    $code=Substr($fd, $start, $length);
which goes through all parts I want to appear in the printer-friendly page, after which some

Code: Select all

echo '<html>
html is echood where the strings appear. This works fine, I get a neat printerfriendly version. However, in the printer-friendly page foo.inc is *not* included, just empty space where it should appear. When I view source, I see the statement

Code: Select all

<?php include 'include/foo.inc'; ?>
but one way or the other it does not show up.
I tried require, require_once, all to no avail.
I wish I understood what was going on.
Thanks for your attention.

Posted: Sat Jul 13, 2002 4:55 am
by gnu2php
OK, now I think I understand the problem--

You have a webpage with a link for people to view a "printer-friendly" version. The HTML has <!--tekst--> and <!--/tekst--> to mark the section where the code is "printer-friendly."

Then when people click the link, they go to a PHP script that reads the "referring URL" from file, and then prints the code that is between <!--tekst--> and <!--/tekst-->.

Am I on the right track?

If so, the problem is--

A PHP script that you open and read (from file) cannot be executed, as far as I know--not even by using the eval() function. When a file is read, it can't be passed through the PHP parser, it'll just print the PHP file exactly as it came.

The only time you can execute an external PHP file is by using the include(), require(), etc., functions. Please correct me if I'm wrong. Therefore, you can't change its contents before outputting it.

Posted: Sat Jul 13, 2002 5:27 am
by salto
Hi Alan,
Thank you. You understood well: indeed, the section between <!--tekst--><!--/tekst--> is printed on the printer friendly page. In this section is the <?php include 'include/foo.inc'; ?> statement, which does appear in the code of the printer friendly page but is not being processed.

I am afraid you are right on the assumption this include cannot be processed since it has gone through the PHP parser already. That seems the most likely explanation. That might explain as well why I don't get an error.

But now I am wondering: there should be a method to let it work, I have seen sites with includes which are processed in the printer friendly version as well. [Yes, of course I asked them- no reply, however]

Your explanation however is very helpful for my understanding of the process. And yes, if you have any ideas .....

Posted: Sat Jul 13, 2002 7:56 am
by PaTTeR
Huh my english is very poor ;-(

So you can solve this problem with this:

you have a string $fd

Code: Select all

$fd = fread(fopen($file, "r"), 100000);
get the name of included file and
replace in this string part <?php include 'include/foo.inc'; ?> with new string $new_string

Code: Select all

$new_string = fread(fopen("foo.inc","r"), 100000)
I hope that you can understand me :wink:

Posted: Sat Jul 13, 2002 8:29 am
by salto
Hi PaTTeR, thank you. I guess I understand what you mean. However; The script takes several strings, in one of which the include statement, from the source page and prints them in the destination page. So the problem is to have the include being *processed* in the new page altogether with the other stuff being printed. Besides this, the script should work site-wide, not only on one page. If that were the case I might find some solution different from PHP and solve the problem for that particular page in the HTML I guess. Anyway, thank you for your time and effort!

Posted: Sat Jul 13, 2002 2:35 pm
by gnu2php
Oh, I'm not Alan Keyes (I'm not THAT smart). He's my hero. His TV show was canceled, by the way.

Back to the subject--

After reading PaTTeR's post, I got an idea. :idea: Use eval() on the code between <?php ... ?>:

Code: Select all

<?php
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000);

if ($fd)
&#123;
	$start = strpos($fd, "<!--tekst-->");
	$finish = strpos($fd, "<!--/tekst-->");
	$length = $finish-$start;

	$code = process_code(Substr($fd, $start, $length));
&#125;


function process_code($html)
&#123;
	// &#123;snip&#125;
	return preg_replace_callback('/<\?php(.*)\?>/i', 'process_php_tag', $html);
&#125;

function process_php_tag($search_array)
&#123;
	// Start the output buffer
	ob_start();

	// &#123;snip&#125;
	eval($search_array&#1111;1]);

	// Get outputted data
	$output = ob_get_contents();

	// Erase output buffer, since we snatched its contents
	ob_end_clean();

	return $output;
&#125;

?>

Posted: Sat Jul 13, 2002 3:27 pm
by salto
Hi 'not Alan but gnu2php', :wink:

Thank you for coming back on this, I appreciate it very much.
Have been playing a bit with your suggestion, but I get parsing errors. I guess it all has to do with the fact that more substrings are processed on the page, not just the $code.
Although I hate to take so much bandwith I copy the full code below so you can see what I mean. Most of the HTML I leave out, but all values necessary are in.

Code: Select all

<?php
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000); 

if ($fd)  
        &#123;  
    $start= strpos($fd, "<!--tekst-->");  
    $finish= strpos($fd, "<!--/tekst-->");  
    $length= $finish-$start;  
    $code=Substr($fd, $start, $length);

    $start= strpos($fd, "<title>");  
    $finish= strpos($fd, "</title>");  
    $length= $finish-$start;  
    $titel=Substr($fd, $start, $length);

    $start= strpos($fd, "<!--updated-->");  
    $finish= strpos($fd, "<!--/updated-->");  
    $length= $finish-$start;  
    $datum=Substr($fd, $start, $length);  
&#125;

echo '<html>
<head>
'.$titel.'</title>
</head>
<body>
'.$code.'
<p>Back:<a href="'.$HTTP_REFERER.'"> '.$HTTP_REFERER.' </a></p>
<p>'.$datum.'</p>
</body>
</html>';  
?>
By the way: to my opinion you can leave the 'newbie' out of your profile and move several levels up!
Thank you for your efforts!

Posted: Sat Jul 13, 2002 4:34 pm
by gnu2php
Oops. Sorry, my fault. Apparently, PHP doesn't like the <?php ... ?> I placed on my comments.

Just remove lines #17 and #26 on the code I posted above. ( :idea: Oh, I forgot, I can just edit my post myself.)

Code: Select all

17:	// Regular Expression for finding all the <?php ... ?> tags
and

Code: Select all

26:	// Process the code between <?php ... ?>

Posted: Sat Jul 13, 2002 6:07 pm
by salto
Thanks gnu2php, Done!
No worry, I removed the php tags in your comments already so that was not the problem. The problem was the order of the statements. First all substrings should be processed and *after* that the functions process_code($html) and process_php_tag($search_array) can be called. Since I messed up terribly in my efforts to get it straigth I just now found out this was the trick.
You did a great job, I would never have found out myself since I am still at the level of small arrays, little substrings and lots of phpinfo() :wink:
It's a pity I am not on the board of this Forum because in that case I could nominate you for a Gold Star, but I guess It won't stay unnoticed.

Thank you so much for your kindness and patience, it is much appreciated!

Posted: Mon Jul 15, 2002 11:09 am
by PaTTeR
It's me again :D

I have an idea, you can try it.

Code: Select all

$fd= fread(fopen("http://yourhost.com/file.php", "r"), 100000);
So file.php will be parsed in PHP. May be this will work.