リンクをクリックしてPHPを実行する
質問
私のウェブサイトのページは、#main divのコンテンツを除いて、すべて同じです(または同じです)。だから私は<!> lt; head <!> gt; <?php $page = "home.html"?>
を実行できます。ここで、home.htmlは裸のコンテンツです(完全なhtmlページではなく、<!> lt; html <!> gt;、<!> lt; head <!> gt;など)。ホームページのメインdiv。したがって、内部では<!> lt; div id = <!> quot; main <!> quot; <!> gt;および<!> lt; / div <!> gt; <?php include($page);?>
があります。この質問をする理由は、リンクがクリックされたときに$ pageを変更したいからです。これどうやってするの?ありがとう。
解決
リンクを次のようにすることができます:
<a href="mypage.php?page=other_page">Other Page</a>
次に、ページの上部でpage
を$_GET['page']
<?php
if (isset($_GET['page']))
{
$page = $_GET['page'] . ".html";
} else {
$page = "home.html";
}
?>
他のヒント
リンクは次のようにする必要があります:
home.php?page=test
次のようにページ変数に値を取得します:
if(isset($_GET['page']))
$page = $_GET['page'].'.html';
else
$page = 'index.html';
次のようなことができます: http://yoursite.com/index.php?page=test.htmlまたは http://yoursite.com/index.php?page=test (後で.htmlを追加します)。
たとえば:
<?php $page = preg_replace('/[^A-Za-z]/','', $_GET['page']);
$path = 'pages/'.$page.'.html'
if (file_exists($path)){ $output = file_get_contents($path); }
else { header("HTTP/1.0 404 Not Found");
$output = file_get_contents('pages/404.html'); }
?>
(404ヘッダーが機能するように、PHPの後にファイルの残りを配置します。)
その後、Apacheリライトを使用することもできます:
Rewrite Engine On
Rewrite Rule ^([A-Za-z]+)$ index.php?page=$1
次に、 http://mysite.com/page_name
のようなリンクを使用しますメインdivで:
<?php echo $output; ?>
所属していません StackOverflow