开发者

Execute PHP on clicking a link

My website's pages are (or will) all be identical, except for the content of the #main div. So I was thinking in the <head> I could do <?php $page = "home.html"?>, where home.html is the bare contents (not a full html page - no <html>, <he开发者_如何学编程ad> etc.) of the main div for the home page. So then inside <div id="main"> and </div> I could have <?php include($page);?>. Then the reason I'm asking this question is I want to change $page when a link is clicked. How can I do this? Thanks.


You could make the links be like this:

<a href="mypage.php?page=other_page">Other Page</a>

Then also in the top of the page where you set page to the value of $_GET['page']

<?php
  if (isset($_GET['page']))
  {
    $page = $_GET['page'] . ".html";
  } else {
    $page = "home.html";
  }
?>


Your link has to be like:

home.php?page=test

You will get the value in your page variable like this:

if(isset($_GET['page']))
  $page = $_GET['page'].'.html';
else
  $page = 'index.html';


You can do something like: http://yoursite.com/index.php?page=test.html or http://yoursite.com/index.php?page=test (and append .html later).

For instance:

<?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'); }
?>

(Putting the rest of your file after the php, for the 404 header to work.)

Then, you also can use apache rewrites:

Rewrite Engine On
Rewrite Rule ^([A-Za-z]+)$ index.php?page=$1

Then use links like http://mysite.com/page_name

Then in the main div:

<?php echo $output; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜