Small PHP CMS base

Posted on

Problem

I am a bit worried about this small PHP base as it has been running a bit slow. Can anyone notice anything majorly wrong with it?

root/Index.php:

define('START', microtime(true));
define('RAM', memory_get_usage(true));
define('ROOT', dirname(__FILE__) . '/');
define("SECURE", true);

ini_set('date.timezone', 'Europe/London');
session_start();

include_once ROOT . '/application/global.php';

Application/global.php:

<?php
require_once 'environment.php';

Application/environment.php

<?php
require_once 'autoloader.php';
require_once 'include/config.php';

if (!isset($config))
    exit('Unable to load configuration data.');

function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

try {
    $dns = "mysql:host=".$config['database']['host'].";dbname=".$config['database']['name']."";
    $pdo = new PDO($dns, $config['database']['username'], $config['database']['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $ex) {
    exit('Unable to connect to the master database! ' . $ex);
}

$c = new Core($config, $_GET['page'], new User($pdo), new Form(), $pdo, new template());

// Google Analytics
/*echo "<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-73890636-1', 'auto');
  ga('send', 'pageview');

</script>";*/
?>

Solution

It is difficult to say by just watching the code lines.

(1)
You can measure line by line. I wrote a simple class to add a message whenever you want. The render method draws an html table an shows the diff time between each message:
http://www.phpclasses.org/package/9282-PHP-Log-and-render-current-script-actions.html

(2)
If the connect time to mysql is about 1 sec and the database is on localhost, then verify mysql settings bind-address in my.cnf. Remove localhost and set the ip, i.e.

    bind-address="127.0.0.1"
    bind-address = ::1

start by changing your syntax values. there too close. mention time break, allow the program to be illogical. also error messages are key to understanding and troubleshooting, please detail the error(s).

Leave a Reply

Your email address will not be published. Required fields are marked *