在 php 中导航页面

问题描述 投票:0回答:9

所以我一直在谷歌上阅读,这只是一堆不同的答案,没有太多解释。

我的问题是如何在 PHP 页面之间切换?假设我的服务器上有一个目录,其中包含以下文件:

index.php
about_us.php
contact_us.php

假设在所有 3 页上,我有一个带有 3 个链接的标题:

Home
Info
Contact

按下其中一个按钮(比方说“联系”)时会发生什么?

我读过 3 种技巧:

Php: header("contact_us.php")
javascript: window.location = "contact_us.php";
html: <meta http-equiv="Refresh" content="5; URL="contact_us.php">

按照今天的标准,这些是否是首选?我在某处读到你现在不应该使用 php 的 header() 函数。

任何见解都会非常有助于我做出决定:)

php navigation
9个回答
10
投票

只需将它们设为常规链接即可

<a href="contact_us.php">Contact</a>

2
投票

只需使用 html 超级参考...

<a href="index.php">Home</a> <a href="contact_us.php">Contact Us</a> <a href="about_us.php">About Us</a>

2
投票

您只需将它们链接为

<a href="contact_us.php">Contact Us</a>

只要点击链接,他们就会被带到该页面。如果您是 PHP 新手:您可以用 PHP 编写 HTML。


1
投票

您也可以使用此技术:(不需要数据库)

假设您有一个index.php文件:

<?php
$mypage = $_GET['mypage'];
switch($mypage)
{
case "one":
    @include("one.php");
    break;

case "two":
    @include("two.php");
    break;

default:
    @include("default.php");
}
?>

然后这样引用:

<a href="index.php?mypage=one">one</a>

And:

<a href="index.php?mypage=two">two</a>

等等

直接调用index.php会将您带到default.php页面内容。


0
投票

您应该直接调用脚本,或者有一个处理程序来调用它(如果您想要好的网址)。

<a href="/contact_us.php">Contact</a>

您不应该使用任何类型的重定向,这会对 SEO 产生不良影响。


0
投票

正如其他人所说,你只需要使用常规的 html 来制作链接即可。

您指的是重定向方法,它无需用户交互即可更改当前位置。如果你想这样做,使用 PHP 的

header()
发送 HTTP 标头绝对是首选方法。


0
投票

我只有解决方案:)

<html>
<body>
<button onclick="confirmNav() ? (doubleConfirmNav() ? navigate() : cancelled() ): cancelled();">Contact Us</button>


<script type="text/javascript">

function confirmNav() {
    var r=confirm("Do you really want to navigate to 'Contact Us'?");
    if (r==true) {
      return true;
    } else {
      return false;
    }
}

function doubleConfirmNav() {
    var r=confirm("Are you 100% sure?");
    if (r==true) {
      return true;
    } else {
      return false;
    }
}

function cancelled() {
    alert("cancelling navigation");   
}

function navigate() {
    // purposely delay the redirect to give the image of a high traffic site
     setTimeout(function() {   
         window.location = "contact_us.php";
     }, 5000);   
 }

</script>

</body>
</html>

0
投票

我总是使用

header("contact_us.php");
。但你可以做
echo "<a href="contact_us.php">Contact</a>";
并将其作为链接。每当我添加 php 链接时,我就是这样做的


0
投票

我有一个问题,因为我不允许发表评论: 使用这样的href标签难道不会构成路径遍历攻击的威胁吗? 我在我的机器上尝试过,通过修改客户端的“href”标签,它实际上返回了我想要的文件。 预先感谢您的帮助

© www.soinside.com 2019 - 2024. All rights reserved.