0

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.

ChrisW
  • 4,970
  • 7
  • 55
  • 92

1 Answers1

2

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

Community
  • 1
  • 1
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • Let me know if I misunderstood your problem. – Ayman Safadi May 21 '12 at 16:36
  • I tried what you said in my systemConfiguration.php: PWDALGO is only going to be used for the passwordStringHandler Class nowhere else is going to use it but I did not want the end user to be rummaging thru the class file to set it. but when I do it the way you said I get undefined constant PWDALGO, when I do it self::PWDALGO or $this->PWDALGO it reverts to my else part of the if/else statment. it should be simple to do this why am i having so much trouble with it. – Phantom Coder May 22 '12 at 00:14
  • I did what you said and it still seemed to not to work and thanks for the link I read that this morning and it did not seem to have what I was trying to do. – Phantom Coder May 22 '12 at 00:39
  • I'm afraid I still don't understand what you're trying to do. The reason you're getting an error when trying my suggestion is that I'm defining the constant outside of the class, therefore `self` and `$this` are not applicable. [View demo here: http://codepad.org/qzZeha5N.](http://codepad.org/qzZeha5N) – Ayman Safadi May 22 '12 at 01:23
  • Also, everything you're saying seems to indicate that everything goes in the `passwordStringHandler` class. Then what exactly goes in the `systemConfiguration.php` file? – Ayman Safadi May 22 '12 at 01:25
  • in the systemConfiguration.php is where I am going to be fefining all the configuration constants, and other items that will be needed for the script or application to run proper, I want it to be the only place for the end user to have to edit. sort of like the php.ini file the only place PWDALGO is going to be called is in the passwordStringHaldler class but it needs to know what encryption method the end user has chosen to use. So I guess the only option I have is to turn systemConfiguration.php into a parent class make a CONSTANT class in it and extend it. – Phantom Coder May 22 '12 at 11:04
  • Don't make it any more complicated that it has to. You were also missing the `pwdsalt` class variable, which could of also triggered the error messages. I still don't understand why this wouldn't work: http://codepad.org/XkkW2ECA. I can't mimic `require()` in any online tool, but I tested this on my PC and it works just fine. – Ayman Safadi May 22 '12 at 13:17