0

I have a front controller which set-ups various elements within the page. However it occurred to me that perhaps some elements were not in the correct order; what I mean by this is that some elements may need to be used/declared prior to other elements.

Or, put another way: have I structured the file in the best/correct way. eg session_start() should come 'after' ini_set() and so on.

ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.use_trans_sid', 0);

$mytimeout = 720 * 60; // (12hrs*60m=720) // minutes * 60
session_set_cookie_params($mytimeout);

$sessdir = "../application/data"; 
ini_set('session.save_path', $sessdir);

session_cache_expire($mytimeout / 60);
ini_set('session.gc_maxlifetime', $mytimeout);

session_start();

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

require_once('../application/models/front.php');
require_once('../application/models/icontroller.php');
require_once('../application/models/view.php');
require_once('../application/models/db.php');
require_once('../application/models/sconfig.php');
require_once('../application/models/acl.php');

require_once('../application/controllers/index.php');
require_once('../application/controllers/user.php');
require_once('../application/controllers/error.php');
require_once('../application/controllers/blog.php');
require_once('../application/controllers/about.php');

function exception_handler($exception) {
  echo "Sorry something went wrong; And or we couldnt find what you requested.<br />We suggest you go back to our <a href=\"/\">home page</a> and try again.<br />We have logged this request in order to provide a better service.<br /> *** ". $exception->getMessage()." ***";

}

set_exception_handler('exception_handler');

$fc = FrontController::getInstance();
$c=$fc->getController();
$a=$fc->getAction();

$users=ACL::getInstance();
$sid=session_id();

if(isset($_SESSION['HTTP_USER_AGENT'])){
  if($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT'])){
    session_regenerate_id(TRUE);
    $_SESSION=array();
    $users->removeUser($sid);
    if(($c!='error')&&($a!='badsession')){
      $fc->redirect("error/badsession");
    }
  }
}
else{
  $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
}   

$dbh=DBConnection::getInstance();

setcookie('ct', 1,0,"/");

$config = Configuration::getInstance();
$cf=array();
$cf=$config->getall();

$users=ACL::getInstance();
$users->addUser();
$users->removeOld();

$front = FrontController::getInstance();

$front->route();
echo $front->getBody();
Chris
  • 81
  • 10
  • Does your code work or does it generates an exception? BTW you have assigned a `FrontController` instance 3 times in the same script. Similar pattern for `ACL`. – Bart Mar 16 '13 at 14:15
  • @Bart code work's fine :) I was just wondering really if I have structured the file in the best way. eg session_start() should come 'after' ini_set() and so on. Yeah true, there are some redundant singletons. – Chris Mar 16 '13 at 16:58

1 Answers1

1

The order of things could be adjusted a tiny bit. This would be the order I choose:

  1. First off all I would place the error_reporting call at the top so you'll have everything reporting errors as expected.
  2. Then set exception_handler and exception_handler so you can catch everything.
  3. Do all the ini_set calls.
  4. require all the necessary scripts.
  5. Start the session with session_start. If all the above things went well.
  6. Do the other logic.

NOTE

It's not a very good idea to use singletons. Especially in the volume you use them. See here why that is What is so bad about singletons? It's better to unlearn the use of singletons. Singletons make you careless about code design and I'm sorry to say. It shows in your example :-)

Community
  • 1
  • 1
Bart
  • 16,076
  • 5
  • 54
  • 79
  • yeah I read about bad practises re singletons from many sites; I guess its like Marmite, you either love it or hate it. lol :) Personally I think they implement composition perfectly. ** Your answer makes sense, although I am thinking that I should do session_start() before requiring anything? – Chris Mar 16 '13 at 20:47
  • If everything is contained in classes then **no**. I would first require and then start the session. Simply because it's not needed. If you might have code depending on a session wich could be executed when the script is included then **yes** – Bart Mar 16 '13 at 21:17