So I'm working on setting up my first "fancy" page system, and I have run into a problem. I use the code seen below, and it loads the "profile" page without any problems, and the default page works fine too. However the other two pages does not show at all, and I can't seem to request them in the URL eighter. All the files are there tho. Any help here will be much appreciated! :)
if(isset($_SESSION['user_id'])){
require('user.php');
$player = new user($_SESSION['user_id'], $database);
$default_page = 'profile';
$pages = array(
'profile' => array(
'name' => 'Profile',
'file' => 'profile.php',
'function' => 'profile',
),
'create_monster' => array(
'name' => 'Create Monster',
'file' => 'monsterPages.php',
'function' => 'createMonster',
),
'create_attack' => array(
'name' => 'Create Attack',
'file' => 'attackPages.php',
'function' => 'createAttack',
),
);
if(!empty($_GET['page'])){
$page = strtolower(trim($_GET['page']));
if(isset($pages[$page])){
require($pages[$page]['file']);
echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
$pages[$page]['function']();
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
You've got some redundancy in your code. You could abbreviate it.
$page = isset($_GET['page']) ? $_GET['page'] : $default_page;
// You should check that the page exists here.
require($pages[$page]['file']);
echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
$pages[$page]['function']();
I suggest you place the page loading code in a function. I've created a simple class here:
<?php
$pages = array(
'profile' => array(
'name' => 'Profile',
'file' => 'profile.php',
'function' => 'profile',
),
'create_monster' => array(
'name' => 'Create Monster',
'file' => 'monsterPages.php',
'function' => 'createMonster',
),
'create_attack' => array(
'name' => 'Create Attack',
'file' => 'attackPages.php',
'function' => 'createAttack',
),
);
class PageLoader
{
public $pages;
public $base_path;
public function __construct($pages, $base_path)
{
$this->pages = $pages;
$this->base_path = $base_path;
}
public function run($name) {
if(! isset($this->pages[$name])) {
throw new Exception('Page not found');
}
$path = $this->base_path . DIRECTORY_SEPARATOR . $this->pages[$name]['file'];
$func = $this->pages[$name]['function'];
require_once $path;
call_user_func($func);
}
}
$page = isset($_GET['page']) ? $_GET['page'] : 'profile'; // If no page given default to profile.
$loader = new PageLoader($pages, '/tmp');
$loader->run($page);
Before you can use $_SESSION, you need to start the session:
session_start(); # this should be the very 1st line in your php
if(isset($_SESSION['user_id'])){
// ...
}
else{
require($pages[$default_page]['file']);
echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
$pages[$default_page]['function']();
}
}
and make sure that $pages[$default_page]['function']();
sets $_SESSION['user_id']