Search code examples
phpperformanceuniqueconstantsprefix

Does defining unique constants for each user cause performance issues in php?


In my script, I am adding some mysql connection data to constants for easier usage. For safety I am trying to make my constant's names less obvious, so the script adds additional unique prefix to each constant every time an user visits the page. My question is - are the constants stored on per-user basis or are they kept in the memory since the last user stops using it on the server and thus, may those additional prefixes for each user make any performance problem or not?

This is the script:

<?php

date_default_timezone_set('UTC');

class misclassified{

    //pseudo function for getting mysql connection data
    private function mysqlData(){
        return array("host"=>"hostname","user"=>"username","base"=>"base");
    }

    public $prefix = "";

    //defines mysql connection constants
    public function mysqlConnConstants(){

        $data = $this->mysqlData();

        $prefix = crypt(date('ymdgis'),"salty pepper always better")."_";
        $prefix = strtoupper($prefix);

        define($prefix."HOST",$data["host"]);
        define($prefix."USER",$data["user"]);
        define($prefix."BASE",$data["base"]);

        $this->prefix = $prefix;

        return true;

    }

}

?>

UPDATE:

It is also intended to use the prefixed variables as check on the beginning on each file used in the project. It would look like this:

<?php
//$prefix - set in main php file where the mysqlMainConstants() is first executed
if(!defined($classReference->prefix."HOST") && !defined($classReference->prefix."USER")): die("Please, try the regular way of using our website!"); endif;
?>

It might be wrong or useless and that's why I ask, because at first it sounded me well as an idea to prevent users from knowing those constants and applying them to the script maliciously.


Solution

  • Assuming you want/need to retrieve the data from a database, then the cost to store the values as constants is not significant. They will be stored in memory for the life of the PHP execution, which is one HTTP request.

    But the more important question is: Why would you do this? It adds no security to the operation and only makes it MUCH more complex environment in which to program.