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
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';
}

Link to comment
Share on other sites

Since your $_GET['page'] variable has the same name as the files, here's a little variation that will handle any page name:

<?php
$page = isset($_GET['page']) && $_GET['page'] != '' ? $_GET['page'] : 'home';
include_once($page.'.php');
?>

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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