Can I include a PHP file in an SHTML file and stor

2019-03-05 20:53发布

问题:

I want to include a PHP file in an SHTML file and store the result in a SHTML variable and include different things depending on the result.

Example:
Assume the PHP script would echo 0/1 in plain text (depending on something like time or something). And then in the SHTML, I'd like to store the output of that PHP file in a SHTML variable. Then if the value stored in that variable is 1, include fileA.html, else include fileB.html.

回答1:

Doesn't sound possible. SHTML uses different methods of parsing the code so I don't think the PHP will run on the page to return a value to the SHTML. Why are you doing this though, why not use PHP outright?



回答2:

Let's say you have a template.shtml file with the text @@PHPVARHERE@@ somewhere in it.

The user calls script.php?var=1. Not the script loads the contents of the template.shtml file (file_get_contents) and does a str_replace to replace @@PHPVARHERE@@ with $_REQUEST['var'] (in this case 1).

Then the script outputs all of this to file1.shtml. If the file already existed use the existing file or overwrite it - whatever happens to be right in this situation.

So the script dynamically creates the relevant shtml-files on the fly at runtime. Just add a Header('Relocate: file1.shtml'); and the script redirects the browser to the generated file.

This is the nearest you can get from what I understood.



回答3:

The answer seems to be NO.

NOTE: It should probably be implemented solely using PHP and a server directive to treat .shtml files as PHP.



回答4:

The way to execute PHP on a .shtml page is to modify your .htaccess file. This file may be hidden, so depending upon your FTP program you may have to modify some settings to see it. Then you just need to add this line for .shtml:

AddType application/x-httpd-php .shtml

If you only plan on including the PHP on one page, it is better to setup this way:

<Files yourpage.shtml>
AddType application/x-httpd-php .shtml
</Files>

This code will only make the PHP executable on the yourpage.shtml file, and not on all of your shtml pages.