0

如果我将常量保留在类代码中,我会制作这个类,但我想从用户可以注释或取消注释 c 常量值的外部文件访问它们。

这种方式效果很好,但我不希望用户在代码中翻来覆去:

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;

}

这可以正常工作,因为它被硬编码到类文件中:

我希望它使用这个来工作:

require ("../configs/systemConfiguration.php");   
class passwordStringHandler
{

我在 if /else 语句中不断得到 else ,它无法找到是否定义了 PWDALGO 。

或者这样

class passwordStringHandler
{
require ("../configs/systemConfiguration.php");

我不知道这是否可能,因为我不断收到错误,我认为您不能在类范围内包含或要求文件。

将来,如果我让它工作,我想要一个安装脚本来检查服务器以查看可用的加密类型并列出供用户选择首选加密方法的列表,然后为他们自动设置. 并且以后可以从管理控制面板更改加密方法。

4

1 回答 1

1

听起来您希望这些常量跨越对象(类),而不是仅限于passwordStringHandler类。

如果这种情况,我建议您使用define()而不是const.

像这样:

系统配置.php

define('PWDALGO', 'whirlpool');

密码字符串处理程序.php

require ("../configs/systemConfiguration.php");

class passwordStringHandler
{
    if ((defined('PWDALGO')) && (PWDALGO === 'md5'))

更多信息: define() vs const

于 2012-05-21T16:36:22.063 回答