Skip to content
Advertisement

How to Set my URL pattern in my php using get method parameters only

I have 3 php files in my directory(/account):

  1. index.php
  2. login.php
  3. signup.php

Accessing the folder in my localhost:

http:localhost/account ==> opens the default(index.php file)

I want to access the login.php and signup.php using:

   => http:localhost/account?login

and

   => http:localhost/account?signup

respectively.

Here is my code in the 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
}

Please help to get me a way to get my URLs like that…

Advertisement

Answer

You have the key login and signup but no value so you should check whether the key exists with 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
}

In case you want to use switch instead,

switch(true) {
    case isset($_GET['login']):
        ... break;
    case isset($_GET['signup']):
        ... break;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement