生成的YouTube去年秋季的ID,在PHP的线性反馈移位寄存器 [英] Generating YouTube-Esque IDs with an LFSR in PHP

查看:182
本文介绍了生成的YouTube去年秋季的ID,在PHP的线性反馈移位寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能扩大从 http://stackoverflow.com/a/9848014/2704706 采取了以下功能为en code /德code号到11个字符的字符串?

Is it possible to scale the following functions taken from http://stackoverflow.com/a/9848014/2704706 to encode/decode numbers into an 11 character string?

function lfsr($x) {
    return ($x >> 1) ^ (($x&1) ? 0xe10000 : 0);
}
function to_4($x) {
    for($i=0;$i<24;$i++)
        $x = lfsr($x);
    $str = pack("CCC", $x >> 16, ($x >> 8) & 0xff, $x & 0xff);
    return base64_encode($str);
}

function rev_lfsr($x) {
    $bit = $x & 0x800000;
    $x = $x ^ ($bit ? 0xe10000 : 0);
    return ($x << 1) + ($bit ? 1 : 0);
}
function from_4($str) {
    $str = base64_decode($str);
    $x = unpack("C*", $str);
    $x = $x[1]*65536 + $x[2] * 256 + $x[3];
    for($i=0;$i<24;$i++)
        $x = rev_lfsr($x);
    return $x;
}

for($i=0; $i<256; $i++) {
    $enc = to_4($i);
    echo $enc . " " . from_4($enc) . "\n";
}

我的最终目标是使用这些方法,形成了连接以类似的方式对视频的标识codeD的ID列入V $ _ GET变量在YouTube上的网址,即RArlg6HeZZM中的 http://www.youtube.com/watch?v=RArlg6HeZZM

由于时间提前。

推荐答案

我不知道是什么YouTube是做什么,但我会做到这一点:随机生成一个固定长度的字符串,试图将其插入到一个表,它的主键,如果你得到一个主键异常错误,产生一个新的字符串,然后再次尝试插入。继续这样做,直到你终于得到成功插入(即一个随机字符串,是不是在该表的话)。其实我用这一个应用程序。

I don't know what youtube is doing, but I would do this: randomly generate a string of a fixed length, attempt to insert it into a table where its the primary key, and if you get a primary key exception error, generate a new string and try to insert again. Keep doing that until you finally get a successful insertion (i.e. a random string that is not in the table already). I actually do use this in one app.

编辑: -

或者你可以添加为一个独特的领域,而不是主键:

Or you can add this as a unique field rather than the primary key:

 alter table tbl add randomstring  varchar(11) unique;

那么你仍然可以有一个AUTO_INCREMENT号作为主键。

Then you can still have an auto_increment number as primary key.

这篇关于生成的YouTube去年秋季的ID,在PHP的线性反馈移位寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆