如何检查加载了哪个 URL 并包含基于此的页面?

发布于 2024-12-12 17:21:14 字数 256 浏览 4 评论 0原文

<?php 
if(...) {
 include("home.php");
} else if(...) {
 include("about.php"); 
}
?>

如果我单击主页按钮,它将包含 home.php 页面,如果我单击 about 按钮,它将包含页面加载的 about.php。我希望采用这种方式,这样我就可以拥有一个 index.php 文件,而不是每一页有多个文件。

有没有办法检查正在加载的网址?

<?php 
if(...) {
 include("home.php");
} else if(...) {
 include("about.php"); 
}
?>

If i click on the home button it will include the home.php page and if i click on the about button it will include the about.php on page load. I want to have it this way so that i can have one index.php file and not multiple ones for each page.

Is there a way to check the url that is being loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So要识趣 2024-12-19 17:21:14

您还可以使用 $_GET 和 switch 语句。首先检查 get var 是否已设置,如果是则使用 switch 语句,否则使用默认值。

<?php 
   if(isset($_GET['page'])) { 
$page = $_GET['page'];
 // roll GET into a var so you can do some error checking if you wish, // 
 // clean it up if need be //
    switch($page) {
        case 'about':
            include('about.php');
            break;
        case 'home':
            include('contact.php');
            break;
        default:
            include('home.php');
            break;
      } 
} else {
  include('home.php');
}
?>

这样,你就被双重覆盖了。如果未设置 get,它会自动包含 home.php,但如果设置了,则有多个预定义选项可供选择。如果有人弄乱了 url,home.php 无论如何都会被包含在内。

PHP switch 语句

You could also use a $_GET and a switch statement. first check to see if the get var is set, and if it is use the switch statement, else, use the default.

something like

<?php 
   if(isset($_GET['page'])) { 
$page = $_GET['page'];
 // roll GET into a var so you can do some error checking if you wish, // 
 // clean it up if need be //
    switch($page) {
        case 'about':
            include('about.php');
            break;
        case 'home':
            include('contact.php');
            break;
        default:
            include('home.php');
            break;
      } 
} else {
  include('home.php');
}
?>

This way, you are double covered. If get isn't set, it automatically includes home.php, but if it is set, you have several predefined options to choose from. if someone is messing with the url, home.php will be included anyway.

PHP switch statement

紙鸢 2024-12-19 17:21:14

您可能还想研究 mod_rewrite。

You might also want to investigate mod_rewrite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文