Jump to content

Open home on index.php


DasterMedia

Recommended Posts

Hi Guys!

 

In order of my little blog-website for my worldtour, I'm having a question about the following.

 

My site starts with an index.html page, with a javascript-referer to 'index2.php?p=home'

This is because i'm stuck with the next:

 

Inside index2.php I'm taking the content from 'stand-alone?'-pages (only with text in it)

I use the next code for it:

 

    <?php            if (isset($_GET['p']))
                    {


                        if($_GET['p']=='home')
                        {
                            include('home.html');
                        }

					elseif($_GET['p']=='page2')
                        {
                            include('page1.html');
                        }

					elseif($_GET['p']=='page1')
                        {
                            include('page2.html');
                        }

                    }?>

 

To show the text from home.html on the page (index2.php) the link must be like this:

www.site.com/index2.php?p=home

 

I'll ask if someone know (and will explain) me how to include 'home.html', without '?p=home'. So I can rename index2.php to index.php, and that home.html automatic will shown when no variabele is behind index.php

 

Thanks!

 

Yours sincerely,

Danny

Link to comment
https://forums.phpfreaks.com/topic/190896-open-home-on-indexphp/
Share on other sites

    {
       include('page2.html');
    } 
} else {
    include('home.html'); //$_GET['p'] doesn't exist
}

 

It's most likely showing standalone text due to the fact it's being included, Therefor all interlinks will be offset to the page it is currently being included to.

A better solution would be a switch, that way you can specify the "default" page (home.html):

 

switch ($_GET['p'])
{
    case 'page1':
        include 'page1.html';
        break;
    case 'page2':
        include 'page2.html';
        break;
    // add more pages here
    default:
        include 'home.html';
}

 

In my opinion though an even better solution for this kind of setup would be to use an array. This would also allow you to add in a 404 style page when the user tries to access pages that don't exist:

 

$valid_pages = array(
    'home'  => 'home.html',
    'page1' => 'page1.html',
    'page2' => 'page2.html'
);

if (!isset($_GET['p']))
{
    $template = 'home.html';
}
else
{
    if (!array_key_exists($_GET['p'], $valid_pages))
    {
        header("HTTP/1.0 404 Not Found");
        $template = 'error.html';
    }
    else
    {
        $template = $valid_pages[$_GET['p']];
    }
}

include $template;

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.