No include in new page
Moderator: General Moderators
No include in new page
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.
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.
Thank you, I am always reluctant to take too much bandwith.
The inlcude is on the page is
That works fine, foo.inc is included.
The pfv.php link opens a printerfriendly version of the page with
which goes through all parts I want to appear in the printer-friendly page, after which some
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 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.
The inlcude is on the page is
Code: Select all
<?php include 'include/foo.inc'; ?>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)
{
$start= strpos($fd, "<!--tekst-->");
$finish= strpos($fd, "<!--/tekst-->");
$length= $finish-$start;
$code=Substr($fd, $start, $length);Code: Select all
echo '<html>Code: Select all
<?php include 'include/foo.inc'; ?>I tried require, require_once, all to no avail.
I wish I understood what was going on.
Thanks for your attention.
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.
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.
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 .....
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 .....
Huh my english is very poor ;-(
So you can solve this problem with this:
you have a string $fd
get the name of included file and
replace in this string part <?php include 'include/foo.inc'; ?> with new string $new_string
I hope that you can understand me 
So you can solve this problem with this:
you have a string $fd
Code: Select all
$fd = fread(fopen($file, "r"), 100000);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)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!
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.
Use eval() on the code between <?php ... ?>:
Back to the subject--
After reading PaTTeR's post, I got an idea.
Code: Select all
<?php
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000);
if ($fd)
{
$start = strpos($fd, "<!--tekst-->");
$finish = strpos($fd, "<!--/tekst-->");
$length = $finish-$start;
$code = process_code(Substr($fd, $start, $length));
}
function process_code($html)
{
// {snip}
return preg_replace_callback('/<\?php(.*)\?>/i', 'process_php_tag', $html);
}
function process_php_tag($search_array)
{
// Start the output buffer
ob_start();
// {snip}
eval($search_arrayї1]);
// Get outputted data
$output = ob_get_contents();
// Erase output buffer, since we snatched its contents
ob_end_clean();
return $output;
}
?>
Last edited by gnu2php on Sat Jul 13, 2002 4:35 pm, edited 1 time in total.
Hi 'not Alan but gnu2php',
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.
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!
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)
{
$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);
}
echo '<html>
<head>
'.$titel.'</title>
</head>
<body>
'.$code.'
<p>Back:<a href="'.$HTTP_REFERER.'"> '.$HTTP_REFERER.' </a></p>
<p>'.$datum.'</p>
</body>
</html>';
?>Thank you for your efforts!
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. (
Oh, I forgot, I can just edit my post myself.)
and
Just remove lines #17 and #26 on the code I posted above. (
Code: Select all
17: // Regular Expression for finding all the <?php ... ?> tagsCode: Select all
26: // Process the code between <?php ... ?>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()
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!
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()
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!
It's me again
I have an idea, you can try it.
So file.php will be parsed in PHP. May be this will work.
I have an idea, you can try it.
Code: Select all
$fd= fread(fopen("http://yourhost.com/file.php", "r"), 100000);