0

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.

Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
  • As you see by tadywankenobi's answer this is not to be combined naturally. Maybe you can use PHP as a SHTML generator or kind of but to give an example/answer you should try to provide small examples of the PHP and SHTML parts which include something. – ZoolWay May 13 '11 at 12:07
  • @Zoolway Just 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. – Adam Lynch May 13 '11 at 13:26

4 Answers4

0

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?

tadywankenobi
  • 745
  • 1
  • 10
  • 28
  • @tadywankenobi the PHP will output 1/0 in plain text so there's no need to parse code. I'm talking about **A LOT** of shtml pages here. Plus I think they are .shtml to speed up Google indexing the site or something like that – Adam Lynch May 13 '11 at 11:33
  • Ah ok, so it's already existing content. What I would recommend, is have run your php code externally to output to another shtml file and include that file. I know it's only 1/0 but at least that way you are keeping your formats consistent. – tadywankenobi May 13 '11 at 11:39
  • Could you elaborate? I need to do two different things depending on the output of the PHP. – Adam Lynch May 13 '11 at 11:48
  • First off, it shouldn't matter to Google Indexing whether the site is in PHP or SHTML. But for time constaints, I'd recommend that you create a PHP file which you can access, say using an AJAX call, that will return the value you need. In fact, in doing it this way, you can skip the include I mentioned earlier, about creating a new SHTML. Would this suit your purposes? Anything, just to prevent the two formats (PHP & SHTML) being parsed at the same time. They're different technologies so shouldn't be run together... – tadywankenobi May 13 '11 at 12:03
  • @tadywankenobi So you're suggestion is to keep the shtml and add some JavaScript that calls the PHP and does things based on the result? I.e. using JavaScript as an intermediary? Sounds good to me. Except if JavaScript is turned off then the PHP script isn't called and the rest of the file is displayed. Unless I get the JS to display "This page needs Javascript by default" and have it output the content if the result of the PHP is the correct one. But then Google can't index the content – Adam Lynch May 13 '11 at 12:12
0

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.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76
  • So does this mean instead of sending the user to page234.shtml I'll send them to page234.php? Which then prints what's the content depending on X? If I'm changing the file user's go to, to PHP, then I might as well just use PHP outright and tell the server to treat SHTML as PHP. Sorry if my question is too confusing – Adam Lynch May 13 '11 at 14:18
  • A simple switch to include another document based on a variable is an easy task for PHP so up to now I am now sure why you use SSI at all. This answer says the user enters script.php?var=**XX**. The script generates page**XX**.shtml if needed and redirects the user. This way you get the page_something_.shtml files you need (if so) but all is controlled by a single PHP script. The shtml files should not contain PHP in this case. As said I still feel you do not need SSI/shtml documents at all. – ZoolWay May 13 '11 at 14:27
  • I agree we shouldn't be using SSI. This is how it has been done, and I am trying to implement this new functionality but I think a sole PHP file with the extension .shtml is the best option. Google is too important; we don't want to change any urls – Adam Lynch May 13 '11 at 14:35
  • If you want old URLs to be still valid, ReWrite them on your webserver (hopefully Apache) to map to your single PHP script! – ZoolWay May 13 '11 at 15:05
  • It's IIS. And we're talking about **A LOT** of pages – Adam Lynch May 13 '11 at 15:06
  • 1
    Then maybe search for URL rewriting on IIS / ask a new question. There is kind of logic in your existing URLs? Like a special part will become one or more variables to your PHP script. So solution to this is: Write a good single PHP script and map your existing URLs with URL-rewriting to the script so that existing links on other pages (and therefor Google) will not break. – ZoolWay May 13 '11 at 15:11
0

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.

Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
0

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.