创建HTML链接到更多HTML代码

时间:2012-09-07 16:41:21

标签: php html

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

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

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

1 个答案:

答案 0 :(得分:1)

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>