Reputation: 3
I have this class i making that works if I keep the constants in the class code, but i wanted to access them from an external file that the user can comment or uncomment out c constant value.
it works great this way but I do not want the users rummaging around in the code:
class passwordStringHandler
{
# const PWDALGO = 'md5';
# const PWDALGO = 'sha1';
# const PWDALGO = 'sha256';
# const PWDALGO = 'sha512';
const PWDALGO = 'whirlpool';
/* THIS METHOD WILL CREATE THE SALTED USER PASSWORD HASH DEPENDING ON WHATS BEEN
DEFINED */
function createUsersPassword()
{
$userspassword = 'Te$t1234';
$saltedpassword='';
if ((defined('self::PWDALGO')) && (self::PWDALGO === 'md5'))
{
$saltedpassword = md5($userspassword . $this->pwdsalt);
echo("The salted md5 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha1')){
$saltedpassword = sha1($userspassword . $this->pwdsalt);
echo("The salted sha1 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha256')){
$saltedpassword = hash('sha256', $userspassword . $this->pwdsalt);
echo("The salted sha256 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha512')){
$saltedpassword = hash('sha512', $userspassword . $this->pwdsalt);
echo("The salted sha512 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'whirlpool')){
$saltedpassword = hash('whirlpool', $userspassword . $this->pwdsalt);
echo("The salted whirlpool generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}
else
echo("No password algro is defined! Edit the [<strong>PWDALGO</strong>] options in the <strong>systemConfiguration.php</strong><br>");
return false;
}
This works ok cause it is hard coded into the class file:
I want it to work using this:
require ("../configs/systemConfiguration.php");
class passwordStringHandler
{
I keep getting the else in my if /else statement it cant find if PWDALGO is defined.
or this way
class passwordStringHandler
{
require ("../configs/systemConfiguration.php");
I do not know if this is possible since i keep getting an error, I do not think you can include or require files inside the class scope.
in the future if I get this to work I wanted a setup script to check the server to see what encryption types are available and to make a list of the for the user to choose there preferred method of encryption, and then set it automatically for them. and be able to change the encryption method later from a admin control panel.
Upvotes: 0
Views: 2160
Reputation: 11552
Sounds like you want these constants to span across objects (classes), and not to be constrained to just the passwordStringHandler
class.
If that is the case, I'd suggest you go with define()
instead of const
.
Like this:
systemconfiguration.php
define('PWDALGO', 'whirlpool');
passwordStringHandler.php
require ("../configs/systemConfiguration.php");
class passwordStringHandler
{
if ((defined('PWDALGO')) && (PWDALGO === 'md5'))
More info here: define() vs const
Upvotes: 2