Jump to content

PHP fread won't parse PHP in Output while Reading .htm files


Recommended Posts

 

I have made a template for websites where php reads .htm files and displays them on the site.

This way all content pages are .htm files and can just be edited via css, etc.. (keeping it simple)

 

Using fread & fopen with php I open the page content file (aboutus.htm for example) and give

the variable $content the entire aboutus.htm file.

To display the page I just echo that variable on the index.php page.

 

Works great except for the fact that if I need to insert ANY <?PHP code; ?> on my content pages

such as aboutus.htm it is displayed just as if it was a .htm page and not a .php. Therefore for example an

echo statement would show up in the source code and not displayed on the page as it should.

 

view source I find

<?php echo 'My php echo statement';?>

 

instead of displaying My php echo statement right on the page.

 

I have tried str_replace when I needed the SID code displayed and that worked for the SID code

but I have tried different options to have the page parse php code unsuccessfully.

 

I am not an expert in PHP and am continually learning by example but I am now out of options.

 

Any suggestions??? I would certainly appreciate it. I am including the code to clarify.

 

 

To call pages I use;

http://mysite.com/index.php?main_page=pagename

 

 

I have a site_include.php that opens .htm files reads them

and puts the page content on variable $content:

 

<?php

/* Content Script; reads .htm files in Content Directory */

 

$mycontentdir = "./content"."/".$mylanguage;

if ($main_page == "") $main_page = "index";

 

$main_page = "" . $main_page . ".htm";

 

if (is_file("$mycontentdir/$main_page")) {

$open = fopen("$mycontentdir/$main_page", "r");

$size = filesize("$mycontentdir/$main_page");

$content = fread($open, $size);

}

?>

 

On my index.php page I just echo $content and the page is displayed where I want it to.

 

When I needed to add a link to a page with the SID code for sessions I included a replace.php file;

 

example link on a content page like:

 

content/aboutus.htm to the contactus.htm page would loook like this:

 

index.php?main_page=contactus&SID_CODE

 

my replace.php does the trick;

 

<?php

/*

Will replace SID_CODE on page with actual SID needed

for links

*/

$sid_code = (SID);

$new_content = array("SID_CODE");

$replacement_code = str_replace($new_content, $sid_code, $content);

$content = $replacement_code;

?>

 

When it comes to using PHP for anything else it remains on the source code and it

is not run/parsed/recognized as php.

 

I have tried replacing characters for <?php etc but just keep getting errors on page.

 

Please help!! Your time is very much appreciated!!!!

 

THANK YOU!

 

Read the description of eval() statement; that's what I was looking for  :)

 

Could you point me in the right direction? The string variable for eval I guess can be $content?

 

Tested but did not work..

 

$new_content=eval($content);

 

Do you suggest I use it in the site_include.php after

 

$content = fread($open, $size);

$new_content = eval($content);

 

or perhaps in the replace.php?

 

Thanks again!!

I assume that by "did not work" you meant you receieve an error? The eval() function expects php code. If you start the string you are evaluating with HTML, you'll get an error. You need to close php first. Try something like:

 

<?php
$content = "<html><head></head><body><?php echo 'hello world!';?></body></html>";
$content = eval('?>'.$content);
?>

You are correct; did give me an error; sorry for not pointing it out;

 

Unexpected '<' in line x on replace.php... etc...

 

I guess it is the 'closing' tag you mention, I don't quite understand why it works putting the ?> as part of the eval.

 

I tested your example on my replace.php file and it worked except for the fact that it is now placing all my .htm file ontop of the page instead of the middle where it is supposed to be.

 

I will play with it and see if I can figure this out, if not I will post a question here again.

 

Thank you for your help! I might have never found the correct php code.

Just in case this helps someone with the same issue I am posting the code that allowed me to control the output.

 

I used an Output Control Function where the $content was being echoed so that it would not run and post the

information before it was needed and therefore posting it in the correct place.

 

Thank you for your help!! Much appreciated.

 

<?php

/*

Output Control Function

This will output the $content data on the correct location because

of the eval() statement it would run the code and display at top of page.

*/

ob_start();

 

$content = eval('?>'.$content);

echo $content;

ob_end_flush();

?>

  • 3 weeks later...

Just when I thought it was out of the park!!!  :-\

 

I was able to control the php parsed output in of the .htm files I am reading and echo'ing;

now I am adding a database for a navigational bar and having the following question;

 

in index.php;

 

include('incl/mysql_dbase.php'); // this file has the queries to the mysql db as well as the db connection

 

include('incl/navbar.php'); // this has the fields needed for example

<?php echo $row_MainNav_RECSET['Nav_pgname']; ?>

 

so far so good but when I add the same record set database echo statement to any .htm file it will not give me the results. Just come out blank or NULL; no errors.

 

Any way to have the db fields be parsed in the .htm file? any suggestions?

 

Your help is certainly appreciated!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.