-1

好的,所以我编写了一个脚本,它将创建一个随机数,检查数据库,如果用户 ID 行中存在数字,它将重新创建一个新数字并继续,直到它找到一个可用的数字......我开始思考,如果我的网站变得巨大,那么它试图随机查找数字并可能不得不不断重复自己,这不是一件可怕的事情......有没有更好的方法来做到这一点,或者这是要走的路线?

用户 ID 不是私人号码,因为它可用于访问用户个人资料……它的设置类似于 Facebook,其中一长串数字或用户名可以访问个人资料。

4

2 回答 2

1

我假设您(无论出于何种原因)试图阻止某人访问类似的页面

profile/<user_id>, 并看到user_id= 100, 然后尝试profile/101, profile/102, 等等, 等等。

如果是这种情况,那么您可以使用这样的东西(以及自动递增的 id)

class Crypt {

    public static function encrypt($data) {

        $config = LoadSomeConfig();

        // open the module to be used.  There are several listed at http://www.php.net/manual/en/mcrypt.ciphers.php     
        $mod = mcrypt_module_open($config->cipher, '', $config->mode, '');

        // use config set initialization vector.  We will use a constant here as we do not want to include this for decryption
        if (isset($config->vector)) {
            $iv = $config->vector;
        } else {
            die("NO IV SET!");
        }

        $key_size = mcrypt_enc_get_key_size($mod);
        $key = substr($config->key, 0, $key_size);
        mcrypt_generic_init($mod, $key, $iv);

        // Do the encryption using the cipher module defined
        $encrypted = mcrypt_generic($mod, $data);

        // cleanup
        mcrypt_generic_deinit($mod);
        mcrypt_module_close($mod);

        // Changed the output based on the config encoding value.  Currently supported values, base64 and hex.
        switch ($config->encoding) {
            case "base64":
                $encrypted = base64_encode($encrypted);
                break;
            case "hex":
                $encrypted = bin2hex($encrypted);
                break;
            default:
                break;
        }
        return $encrypted;
    }

    public static function decrypt($data){

        if (empty($data)) {
            return '';
        }

        // config options set include the cipher, mode and secret key
        $config = LoadSomeConfig(); 

        // Change encrypted data base to binary based on the encoding mechanism used to generate the data
        switch ($config->encoding) {
            case "base64":
                $data = base64_decode($data);
                break;
            case "hex":
                $data = pack("H*", $data);
                break;
            default:
                break;
        }


        if (isset($config->vector)) {
            $iv = $config->vector;
        } else {
            die("NO IV SET!");
        }

        $mod = mcrypt_module_open($config->cipher, '', $config->mode, '');
        $key_size = mcrypt_enc_get_key_size($mod);
        // max key size is 448 bits
        $key = substr($config->key, 0, $key_size);
        mcrypt_generic_init($mod, $key, $iv);

        // decrypt the data
        $decrypted = mdecrypt_generic($mod, $data);

        // cleanup
        mcrypt_generic_deinit($mod);
        mcrypt_module_close($mod);

        return trim($decrypted);
    }
}

然后你会有一个像profile/c2ffd340ea3b71ca065e6add4143f36d

在您的个人资料页面中,假设 user_id 可以在 中访问user_id,您可以简单地执行以下操作:

$user_id = Crypt::decrypt($user_id);

并像往常一样进行。创建指向某人的个人资料页面的链接时,您将使用类似profile/<?php echo Crypt::encrypt($user->user_id); ?>

于 2012-11-27T03:00:42.543 回答
0

由于您的用户名是唯一的(我猜),也许让 /profile/ 控制器从 URL 中获取用户名并加载配置文件会更好。

另一种方式是,您不仅在我们的代码中添加了混淆的东西——如果您的加密代码由于某种原因发生了变化怎么办?所有外部链接、书签等将不再起作用。而且您将拥有更好的搜索引擎覆盖率,因为 URL 中的用户名比一些神秘的东西更有价值。啊,是的,当您决定散列时,您选择的任何算法都不会发生理论上可能的冲突;)

于 2012-11-27T03:27:00.460 回答