Jump to content

[HELP] Making a default page appear


FatDank

Recommended Posts

Hi all. Can someone help me please. Below is the code I have.

 

What I want is instead of having to go index.php?page=home

I just want to go to the root folder so say example.com/ and it will display what ?page=home is.

 

So just to clarify;

 

I want this:  example.com/index.php?page=home

To be this: example.com

 

<?php 
if ($_GET['page'] == "home"){
   include 'status.php';
}else if ($_GET['page'] == "todays"){
   include("todays.php");
}else{
   // . . .
}
?>

Link to comment
https://forums.phpfreaks.com/topic/240457-help-making-a-default-page-appear/
Share on other sites

You could do this:

 

$page = (isset($_GET['page'])) ?: 'home';

 

That will only work with PHP 5.3+ though, so if you are using something lower than that you would have to do this:

 

$page = (isset($_GET['page'])) ? $_GET['page'] : 'home';

 

 

After that you just use $page instead of $_GET['page'] in your if statements:

 


$page = (isset($_GET['page'])) ?: 'home';

if ($page == 'home') {
    // include home page
}
if ($page == 'someotherpage') {
    // include some other page
}

 

 

In case you don't know, ? : is called a ternary operator. It's basically like a mini if statement. It does the same thing as this if statement:

if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 'home';
}

Personally I'd use a white list array ad do it this way:

 

 

$page      = isset($_GET['page']) ? strtolower($_GET['page']) : 'home';


$whiteList = array('home','about','contact');


if ( in_array($page, $whiteList) )
{
   include $page . '.php';
}

You could also do:

switch($_GET['page'])
{
   case 'about':
      include 'about.php';
   break;
   case 'contact':
      include 'contact.php';
   break;
   default:
      include 'home.php';
   break;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.