Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3631716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:25:58+00:00 2026-05-19T00:25:58+00:00

I using the following as Frontpage/Page Controller(s) and it’s working ok so far, except

  • 0

I using the following as Frontpage/Page Controller(s) and it’s working ok so far, except two problems I’m facing which as you can see are the $pages array and the switch, which are actually much much longer as the one I’ve pasted here. Everytime there is a need for a new page controller I have to add it to $pages array and to switch which makes that list very long. How would you overcome this problem and do you see any other improvement on this code? loadLogic() in page controllers it is used to get functions under pages/controllername/logic/function.php.

Frontpage Controller – index.php:

include 'common/common.php';
if(!isset($_GET['p']) OR $_GET['p'] == ''){
    $_GET['p'] = 'home';
    header('Location: index.php?p=home');
}

$pages = array('home','register','login','logout','page1','page2','page3');

$_GET['p'] = trim($_GET['p']);

if(isset($_GET['p'])){
    if(in_array($_GET['p'], $pages)){
        switch ($_GET['p']) {
            case 'home':
                include 'home.php';
                break;
            case 'register':
                include 'register.php';
                break;
            case 'login':
                include 'login.php';
                break;
            case 'logout':
                include 'logout.php';
                break;
            case 'page1':
                include 'page1.php';
                break;
            case 'page2':
                include 'page2.php';
                break;
            case 'page3':
                include 'page3.php';
                break;
        }
    }else{
        echo '404!';
    }
}

Page Controller – {home,register,login,logout,page1,page2,page3}.php:

include 'tpl/common/header.php';
contentStart();

if(isset($_SESSION['logged'])){
    loadLogic('dashboard');

}else{
    loadLogic('nologin');

}


//Display login form in logic page instead links
//
if(!isset($_SESSION['logged'])){
    contentEnd();
    loadLogic('nologinForm');
}else{
  contentEnd();
  include'tpl/common/rcol.php';

}
include 'tpl/common/footer.php';

function loadLogic():

function loadLogic($logic) {
    $path = dirname(__DIR__) . '/pages';

    $controller = preg_split('/&/',$_SERVER['QUERY_STRING']);
    $controller = trim($controller[0],"p=");
    $logicPath = 'logic';
    $logic = $logic . '.php';
    $err = 0;
    $logicFullPath = $path.'/'.$controller.'/'.$logicPath.'/'.$logic;

    if($err == '0'){
        include "$logicFullPath";

    }
}

Folder Structure:

projectName
  |
   ---> common
  |
   ---> pages
  |   |
  |    --->home
  |   |
  |    --->register
  |   |
  |    --->login
  |   |
  |    --->logout
  |   |
  |    --->page1
  |   |
  |    --->page2
  |   |
  |    --->page3
  |
   ---> tpl
  |   |
  |    ---> common
  |
   --> home.php
  |
   --> register.php
  |
   --> login.php
  |
   --> logout.php
  |
   --> page1.php
  |
   --> page2.php
  |
   --> page3.php
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:25:59+00:00Added an answer on May 19, 2026 at 12:25 am

    For the frontpage controller, why so many case statements. You already know what pages are valid to include and you check if it’s in the valid pages.

    You can just do:

    if(isset($_GET['p']))
    {
        if(in_array($_GET['p'], $pages))
        {
             include($_GET['p'] . '.php');
        }
    }
    

    If you wanted it to have different names passed to $_GET for obfuscation, along with different potential extensions, then you could do:

    $pages = array('home'=>'index.php','register'=>'registerpage.htm','page1'=>'one.html');
    
    if(isset($_GET['p']))
    {
        if(array_key_exists($_GET['p'],$pages))
        {
              include($pages[$_GET['p']]);
        }
    }
    

    If you wanted to make the array of pages more easily managable you can break it up into multiple lines:

    $pages = array(
    'home'=>'index.php',
    'register'=>'registerpage.htm',
    'etc'=>'/home/user/public_html/directory/etc.php'
    );
    

    Oh, also, since it’s sort of ugly to have the home page be http://www.domain.com/?p=home, just make home be the default include if the value for p is not in the array or not an array key, depending on which you’d use.

    So:

    if(isset($_GET['p']) && in_array($_GET['p'],$pages)) //You can combine these like this as well, same functionality.  If p isn't set, it won't even try the in_array()
    {
        include($_GET['p'] . '.php');
    }
    else
    {
        include('home.php');
    }
    

    Then you can get rid of that if !isset $_GET[‘p’] or $_GET[‘p’] == ” at the top. You can also combine the isset and in_array/array_key_exists into the same if statement with an &&. If the first evaluation is false and it hits an && then it just stops and doesn’t evaluate the rest, so no errors or anything, and it also means that you can easily set a default response just once, as having them nested means that you’d have to have a default for both ifs.

    More edits. If you really wanted to have a 404 when a user tries to go to a p= that’s non existant instead of just getting booted to the home page, you could do this at the top:

    if(isset($_GET['p']))
    {
    $fourohfour = true; 
    }
    

    and then down in the If structure to include the pages, do an else if before the else include home.php like:

    else if(isset($fourohfour))
    {
        include('404.php');
    }
    

    So that if p is set but doesn’t check out then it will include 404, but if it isn’t set, it will go to home.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code to append param to url. This is working fine
I am using following code to connect with WPA2 in android (I can connect
Using following query in my procedure I can count to connection to my database
Using following regexp I can get if a string starts with a or b
Using following controller: class mydvbController extends Zend_Controller_Action { public function indexAction() { } public
Using following code I try to get updated list of checkbuttons' corresponding text values,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I am using following configuration to properly fit image inside a scrollview. <?xml version=1.0
I am using following code for showing a MessageBox with ok and cancel button.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.