Home > Enterprise >  How to use URL parameters to have multiple pages in one page
How to use URL parameters to have multiple pages in one page

Time:07-25

I have a home page and I want some other pages like the about us page and contact us page to be in this page to reduce the amount of files and to have a less messy URL. I want something like www.example.com/index.php?page=about to open up about us page and similarly www.example.com/index.php?page=contact to open up contact us page.

How can I use URL parameters like these to have multiple pages in one page?

CodePudding user response:

You need to use $_GET like this

$par = filter_var ($_GET ['page'] ?? '', FILTER_SANITIZE_STRING); 

The ?? '' gives you a blank string if the parameter is missing. And you need to sanitize any GET parameters against attacks.

Then it depends on what you want.

You can include a file

if ($page=='about')
{require_once 'about.php';}

Or you can change the url

if ($page=='about')
{header('Location: about.php');}

If you do the latter, then this has to be before any html is output or echoed.

  • Related