如何仅使用 get 方法参数在我的 php 中设置我的 URL 模式

问题描述

我的目录 (/account) 中有 3 个 PHP 文件

  1. index.PHP
  2. 登录.PHP
  3. 注册.PHP

访问我本地主机中的文件夹:

http:localhost/account ==> 打开认(index.PHP 文件

我想访问 login.PHP 和 signup.PHP 使用:

   => http:localhost/account?login

   => http:localhost/account?signup

分别。

这是我在 index.PHP 中的代码

<?PHP 
if($_GET['login']){
        include('login.PHP')
} else {
        // Load homepage of the site
        // which in my case http://localhost
}
if($_GET['signup']){
        include('signup.PHP')
} else {
        // Load homepage of the site
        // which in my case http://localhost
}

请帮助我找到一种方法获取我的 URL...

解决方法

您拥有登录和注册密钥但没有值,因此您应该使用 isset() 检查密钥是否存在。

<?php 
if(isset($_GET['login'])){
        include('login.php')
} else {
        // Load homepage of the site
        // which in my case http://localhost
}
if(isset($_GET['signup'])){
        include('signup.php')
} else {
        // Load homepage of the site
        // which in my case http://localhost
}

如果您想改用 switch

switch(true) {
    case isset($_GET['login']):
        ... break;
    case isset($_GET['signup']):
        ... break;
}