0

サーバーには、ファイルを含めるための次のステートメントを含む多数の .shtml ファイルがあり.incます。

 <!--#include virtual="../v4/rightmenu.inc" -->

これはまさに、問題なく動作しているソースに表示される方法です。

現在のファイルにはこの種のインクルージョンが多く含まれており、このような多くのコードをいじりたくないので、これを変更せずに php コードで実行できるかどうか疑問に思っています。

私はそれを次のようなものに変更したくないだけです<?php include "../v4/rightmenu.inc"; ?>

4

1 に答える 1

0

Use mod_rewrite to route all requests to .shtml files through a single php file. For exapmle: _includeParser.php

A very rough sketch of such a file could be:

/**
 * This function should return the contents of the included file
 */
function getIncludeFileContents($match) {
    list (, $file) = $match;
    // return something for $file
}

/**
 * This function should return a php-readable path for the initially requested .shtml file
 */
function getRealpath($uri) {
  // return the real path of the requested uri
}

// Map the requested uri to file
// 'REDIRECT_URL' could be something different for your configuration, have a look at the $_SERVER array
// to get the correct value if something fails here
$realpath = getRealpath($_SERVER['REDIRECT_URL']); 

// parse each include statement
// the regex pattern here could need some tweaking
$output = preg_replace_callback(
  '@<!--#include virtual="(.+?)" -->@i',
  'getIncludeFileContents',
  file_get_contents($realpath)
);

// output the final contents
echo $output;

Of course a solution like this could be optimized by some caching or other methods.

于 2011-06-30T16:19:42.183 に答える