【问题标题】:Is a script that creates an number, searches database for the number, and repeat until it finds a unique number a bad script to use? [closed]一个创建号码、搜索数据库以查找号码并重复直到找到唯一号码的脚本是否是一个不好的脚本? [关闭]
【发布时间】:2012-11-27 02:34:56
【问题描述】:

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

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

【问题讨论】:

  • AUTOINCREMENT 或 GUID 之类的有什么问题?
  • 所以您是第一个为数据库行创建唯一 ID 的人?
  • 我用它来做用户ID,有人提到如果是公众号,使用AI可能不是最好的用户ID。
  • 完全取决于你如何使用它。
  • 基本上是一个会员注册,他们得到一个唯一的用户ID。如果我得到很多注册,我只是不想开始遇到麻烦。第一次尝试以最好的方式做所有事情。

标签: php mysql numbers unique


【解决方案1】:

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

profile/<user_id>,看到user_id = 100,然后尝试profile/101profile/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); ?>

【讨论】:

  • 感谢您的帮助,这正是我在回答我的问题时所要寻找的。​​span>
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多