John Davidson

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

0 comments
Message:


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...



My Answer:
To set up URL patterns in PHP using GET method parameters only, you can use the following approach:

1. Create an .htaccess file in the root directory of your project (if it doesn't already exist) and add the following code to enable URL rewriting:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]


2. In your index.php file, you can then parse the URL parameter and include the corresponding PHP file based on the parameter value. Here is an example code snippet:

php
if(isset($_GET['url'])) {
$url = $_GET['url'];
switch($url) {
case 'login':
include 'login.php';
break;
case 'signup':
include 'signup.php';
break;
default:
include 'index.php';
break;
}
} else {
include 'index.php';
}
?>


3. Now you can access the different pages by appending the page name to the URL. For example:

- http://localhost/account/login
- http://localhost/account/signup

This way, you can set up URL patterns in PHP using GET method parameters only.

Rate this post

4 of 5 based on 5465 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us