Richy
Richy

Reputation: 69

Best Solution for PHP Index.php Contoller

What's the best way of including PHP files to link all php files together to allow for arrays to pass through each file.

Currently I'm am using a switch to include a html and php file when a case value is passed through from a .htaccess file.

.HTACCESS

RewriteRule ^$ index.php?page=homepage
RewriteRule ^login$ index.php?page=login

INDEX.PHP

if(isset($_GET['page'])) {
    switch($_GET['page']) {
        case 'login':
            include('login.php');
            include('login.html');
        break;

        default:
            include('index.html');
        break;
    }
}

This technique doesn't seem like the best solution, as you will eventually end up with a huge switch statement containing all pages for the site.

Is there a better solution?

Also, I am having issues passing an array through to a folder called 'account' the array will contain details of who is logged in etc, but as soon as I add code to the folder, the array from the main site doesn't get included.

How do I access an array from outisde the folder inside the 'account' folder?

Upvotes: 0

Views: 47

Answers (1)

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

To avoid a large switch you can do this:

if(isset($_GET['page']) === true)
   if(file_exists($_GET['page'] . '.html') === true) {
       include($_GET['page'] . '.php');
       include($_GET['page'] . '.html');
    } else {
       header('HTTP/1.0 404 Not Found');
       echo 'Page not found';
       exit;
    }
} else {
   include('index.html');
}

This limits you that the .html and the .php file have to be named same as the according page GET parameter.

Upvotes: 1

Related Questions