创建指向更多HTML代码的HTML链接


Create HTML Link To More HTML Code

我有生成html网站的file1.php。我有file2.html,它只包含html代码。是否可以在file1.php中创建一个生成的超链接来链接到file2.html中的代码?我希望file1.php生成一个不依赖于file2.html是否存在的html网站。

我可以使用php的file_get_contents函数获取file2.html的内容,但如何超链接到该函数返回中保存的代码?

例如,我会单击超链接,然后看到file2.html,就像我使用任何web浏览器打开file2.html一样。我希望它看起来像是在链接到一个外部文件(file2.html),但实际上,在执行file1.php生成的html网站时,就好像file2.html不存在一样。

file1.php

<a href="show_contents.php?file=file2.html">Link</a>

show_contents.php

// perform checks to make sure $_GET['file'] exists then display contents
// you will also have to do some validation on the variable passed in to file so 
// the user can't change the path to be whatever they want
echo file_get_contents($_GET['file'])

根据评论更新:

file1.php

if (isset($_GET['file')) {
    // perform checks to make sure $_GET['file'] exists then display contents
    // you will also have to do some validation on the variable passed in to file so 
    // the user can't change the path to be whatever they want
    echo file_get_contents($_GET['file'])
    exit();
}
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?file=file2.html">Link</a>