0

htmlWebサイトを生成するfile1.phpがあります。htmlコードのみを含むfile2.htmlがあります。file1.php内に生成されたハイパーリンクを作成して、file2.html内のコードにリンクすることは可能ですか?file1.phpに、file2.htmlが存在するかどうかに依存しないhtmlWebサイトを生成させたい。

phpのfile_get_contents関数を使用してfile2.htmlのコンテンツを取得できますが、その関数の戻り値内に保存されたコードにハイパーリンクするにはどうすればよいですか?

たとえば、ハイパーリンクをクリックすると、Webブラウザを使用してfile2.htmlを開いた場合と同じようにfile2.htmlが表示されます。外部ファイル(file2.html)にリンクしているように見せたいのですが、実際には、file1.phpが生成したhtmlWebサイトの実行中にfile2.htmlが存在しないかのように機能します。

4

1 に答える 1

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>
于 2012-09-07T16:45:19.493 に答える