【问题标题】:using $_GET without using '?'使用 $_GET 而不使用 '?'
【发布时间】:2016-03-12 14:32:35
【问题描述】:

我想创建一个在 PHP 中被 $_GET 捕获的链接。让我解释一下:

目前我在 HTML 中执行此操作:

<a href="?contactus">Contact Us</a>

这就是我在 php 中所做的:

<?php
    if (isset($_GET['contactus'])){
        include "contactus.php";
    }
?>

在这种情况下,地址变为www.domain.com/?contactus

但我想要这样的地址:www.domain.com/contactus

如果我将 html 更改为 &lt;a href="contactus"&gt;Contact Us&lt;/a&gt; 但它不起作用。任何人请告诉我。谢谢

【问题讨论】:

  • 你要求重写 url,它应该由你的 HTTP 服务器设置
  • 搜索(Google、StackOverflow 等)“URL 重写”。网络上有很多资源,您可以在其中了解您需要在 .htaccess 中编写的内容以使其发挥作用。

标签: php html


【解决方案1】:

您需要一个 .htaccess 文件才能使其工作

像这样:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^contactus\/?/?$ index.php?contactus
</IfModule>

和 index.php 这样的

<?php
    if (isset($_GET['contactus'])){
        include "contactus.php";
    }
?>

【讨论】:

    【解决方案2】:

    您可以使用 domain.com/contactus 并解析 URL

    $url = "http://domain.com/contactus";
    $url = parse_url($url);
    $path = $url['path']; // contactus
    echo $path;  // contactus
    

    然后你就可以使用它了

     if ($path == "contactus"){
           include "contactus.php";
        }
    

    您仍然需要重写 URL,尝试添加或编辑您的 .htaccess 文件

    RewriteEngine On    # Turn on the rewriting engine
    RewriteRule    ^contactus/?$    contactus-page.php    [NC,L]    # Handle requests for "contactus"
    

    【讨论】:

    • 它仍然需要 URL 重写。否则服务器不知道如何处理 URL 并返回404 Not Found(即您建议的代码没有任何机会运行)。
    【解决方案3】:

    嗨,你需要这样做 在您设置锚标记的页面上执行以下操作 $url="contact.php?page"='contactus'; 给 href="$url" 。

    另外一面你可以使用$urlget = $_GET['page'];

    请注意你使用的属性是页面所以当你尝试进入另一个页面时你需要使用$_GET['page'] 当你点击地址栏中的联系我们页面时你可以看到url 还有contact.php? page='contactus' 显示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 2016-06-15
      相关资源
      最近更新 更多