redis實現發號器,redis實現發號

來源:互聯網
上載者:User

redis實現發號器,redis實現發號

通過mysql的auto increment自增id值可能會泄漏一些敏感的資料。

例如使用者表的user_id是自增的,在url中顯示的id值可能就泄露了網站真實的使用者數。

下面代碼通過php及redis的incrby實現簡單的發號器,代碼如下:

function get_id($type, $server_ip, $server_port, $key) {    $init_num = 0;    $redis= new Redis();    $redis->connect($server_ip, $server_port);    $var = $redis->exists($key);    if($var == 0) {        $redis->set($key,$init_num);    }    $incr_num = rand(1,50); //增量為隨機數    $var = $redis->incrby($key, $incr_num);    $redis->close();    return $var;}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.