Как запустить shtml включить в php - PullRequest
0 голосов
/ 30 июня 2011

У меня есть десятки файлов .shtml на сервере, которые включают в себя это утверждение, включающее .inc файлы:

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

Это именно то, как это показано на источнике, который работает просто отлично.*

Мне интересно, смогу ли я запустить это на своем php-коде, не меняя его, поскольку в текущих файлах такого рода много включений, и я не хочу связываться с большим количеством кода, подобного этому.

Я просто не хочу менять его на что-то вроде <?php include "../v4/rightmenu.inc"; ?>

1 Ответ

0 голосов
/ 30 июня 2011

Используйте mod_rewrite для маршрутизации всех запросов к .shtml файлам через один файл php. Например: _includeParser.php

Очень грубый набросок такого файла может быть:

/**
 * 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;

Конечно, подобное решение может быть оптимизировано с помощью некоторых методов кэширования или других методов.

...