Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP 5.4+] Tips and tricks!

Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
128
Hello in this tutorial i will show you some basic tips and tricks.

1. Default session variable assignment...

Most of you(including me at first :D) sets default variables for session like this:
PHP:
if(!isset($_SESSION['token']))
{
    $_SESSION['token'] = md5(uniqid(rand(), true));
}
if(!isset($_SESSION['language']))
{
    $_SESSION['language'] = 'english';
}
if(!isset($_SESSION['access']))
{
    $_SESSION['access'] = 'guests';
}
Problem: To much checks each time page is loaded, as these are needed to load only once, there is nothing wrong with this at all but we can make this better and more readable.
Solution: Lets add only one isset check for user first visit.
PHP:
if(!isset($_SESSION['active']))
{
    $_SESSION['signed']     = false;
    $_SESSION['language']   = 'english';
    $_SESSION['template']   = false;
    $_SESSION['token']      = md5(uniqid(time()));

    $_SESSION['active']     = true;
}

2. Class auto loading
Class auto loading could be expensive but there are always pros and cons. One of the huge impact could be that php would load only classes when you require them. And you do not need to include them manualy...
PHP:
spl_autoload_register(function($class){
    $name = str_replace('_',DIRECTORY_SEPARATOR,$class);

    include $name.'.php';

    if(method_exists($class,'autoLoad')){
        $class::autoLoad();
    }
});
Class naming example using this code: for example you have class in folder: "sys/core/module.php" the class name will be: "sys_core_module". You should avoid over complicate auto loading functions... This auto loader will load public static function autoLoad() if it exist in class.


3. Class/function/language/config loading

Using loader code you do not need initialize new class or include config file over and over if config file or class is already loaded it's reference is returned for you..

PHP:
class sys_core_loader {

    /**
     * @var array
     */
    public static $extensions     = array();
    /**
     * @var array
     */
    public static $languages      = array();
    /**
     * @var array
     */
    public static $configurations = array();

    /**
     * @param $name
     * @return mixed
     */
    public static function load($name)
    {
        if(!isset(self::$extensions[$name])){
            self::$extensions[$name] = new $name;
        }
        return self::$extensions[$name];
    }

    /**
     * @param   string  $name
     * @param   bool    $onFailDefault
     * @return  bool    false if language file do not exists
     *          null    null if language file is empty or incorrect format
     *          array   array if language file was loaded
     *
     */
    public static function loadLanguage($name,$onFailDefault = true)
    {
        if(!isset(self::$languages[$name])){
            if(is_file(X_PATH_APP.'language/'.$_SESSION['language'].'/'.$name.'.php')){
                $language = NULL;

                include X_PATH_APP.'language/'.$_SESSION['language'].'/'.$name.'.php';

                self::$languages[$name] = $language;
            }
            else{
                if($onFailDefault == true){

                    $config = self::loadConfiguration('common');

                    if(is_file(X_PATH_APP.'language/'.$config['language'].'/'.$name.'.php')){
                        $language = NULL;

                        include X_PATH_APP.'language/'.$config['language'].'/'.$name.'.php';

                        self::$languages[$name] = $language;
                    }
                }
                else{
                    return false;
                }
            }
        }
        return self::$languages[$name];
    }

    /**
     * @param $name
     * @return bool
     */
    public static function loadConfiguration($name)
    {
        if(!isset(self::$configurations[$name])){
            if(is_file(X_PATH_APP.'config/'.$name.'.php')){
                $config = NULL;

                include X_PATH_APP.'config/'.$name.'.php';

                self::$configurations[$name] = $config;
            }
            else{
                return false;
            }
        }
        return self::$configurations[$name];
    }
}

4. Resolving full web location paths.

This following script will return full path to current web public_html folder including subfolders

this must be put in index.php file!

PHP:
define('X_PATH_BASE',     dirname(__FILE__));
define('X_PATH_SYSTEM',   X_PATH_BASE.'/sys/');
define('X_PATH_ASSETS',   X_PATH_BASE.'view/assets/');
define('X_PATH_APP',      X_PATH_BASE.'app/');

include X_PATH_SYSTEM.'system.php';
 
Initiate Mage
Joined
Nov 27, 2011
Messages
3
Reaction score
0
There`s no such thing like too much checks when page loads...

PHP:
if(!isset($_SESSION['active']))
{
    $_SESSION['signed']     = false;
    $_SESSION['language']   = 'english';
    $_SESSION['template']   = false;
    $_SESSION['token']      = md5(uniqid(time()));

    $_SESSION['active']     = true;
}

Dont you think this method is better:

PHP:
$_SESSION['signed'] = isset($_SESSION['signed']) ? $_SESSION['signed'] : false;
$_SESSION['language'] = isset($_SESSION['language']) ? $_SESSION['language'] : english;
$_SESSION['template'] = isset($_SESSION['template']) ? $_SESSION['template'] : false;
$_SESSION['token'] = isset($_SESSION['token']) ? $_SESSION['token'] : md5(uniqid(time()));
 
Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
128
There`s no such thing like too much checks when page loads...

PHP:
if(!isset($_SESSION['active']))
{
    $_SESSION['signed']     = false;
    $_SESSION['language']   = 'english';
    $_SESSION['template']   = false;
    $_SESSION['token']      = md5(uniqid(time()));

    $_SESSION['active']     = true;
}

Dont you think this method is better:

PHP:
$_SESSION['signed'] = isset($_SESSION['signed']) ? $_SESSION['signed'] : false;
$_SESSION['language'] = isset($_SESSION['language']) ? $_SESSION['language'] : english;
$_SESSION['template'] = isset($_SESSION['template']) ? $_SESSION['template'] : false;
$_SESSION['token'] = isset($_SESSION['token']) ? $_SESSION['token'] : md5(uniqid(time()));

first one was more readable. but it really do not even matter... only thing that matters is what is easier for you... it's a matter of taste...
 
Initiate Mage
Joined
Nov 27, 2011
Messages
3
Reaction score
0
And a matter of removing "active" session wich is no longer needed.
 
Intelligent DoucheBag
Loyal Member
Joined
Jan 5, 2008
Messages
1,698
Reaction score
288
Hello in this tutorial i will show you some basic tips and tricks.

1. Default session variable assignment...

Most of you(including me at first :D) sets default variables for session like this:
PHP:
if(!isset($_SESSION['token']))
{
    $_SESSION['token'] = md5(uniqid(rand(), true));
}
if(!isset($_SESSION['language']))
{
    $_SESSION['language'] = 'english';
}
if(!isset($_SESSION['access']))
{
    $_SESSION['access'] = 'guests';
}
Problem: To much checks each time page is loaded, as these are needed to load only once, there is nothing wrong with this at all but we can make this better and more readable.
Solution: Lets add only one isset check for user first visit.
PHP:
if(!isset($_SESSION['active']))
{
    $_SESSION['signed']     = false;
    $_SESSION['language']   = 'english';
    $_SESSION['template']   = false;
    $_SESSION['token']      = md5(uniqid(time()));

    $_SESSION['active']     = true;
}

2. Class auto loading
Class auto loading could be expensive but there are always pros and cons. One of the huge impact could be that php would load only classes when you require them. And you do not need to include them manualy...
PHP:
spl_autoload_register(function($class){
    $name = str_replace('_',DIRECTORY_SEPARATOR,$class);

    include $name.'.php';

    if(method_exists($class,'autoLoad')){
        $class::autoLoad();
    }
});

Okay, I go to a website and I got those sessions, good.
But I delete only one session through debug mode.
It will not be set again, because sessions are active, but it doesn't exist.

Ps.
Browsers tend to delete unused sessions, if a session is set but is only used on a page that will be barely visited.
The browser will delete that one session, but it won't be re-set because the sessions are still active.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
True, but the phpsession cookie is stored on the client. (jur13n is still wrong anyway)

I'm well aware of that but that doesn't change the fact that you can't remove one single session. You can clear it, sure, but that's about it
 
Back
Top