开发者

Creating a small encrypted string in PHP

I am looking to create a small encrypted string, like the referral strings used by Twitpic or bit.ly, for 开发者_如何转开发a website I am working on for referral purposes. Any of the built-in functions like MD5 and mcrypt each make strings that are too long for my purposes.

Is there an easy way to create a string like this? Thanks.


From the question, I'm guessing that you just want to have a short string of text/numbers that uniquely refers to a username, an image, an URL or something similar.

A solution would be just to generate a random string and map that to the user/image/URL in your database. Here's the random string function we use. You can adjust $chars depending on your application. It can generate a short 5-letter string like twitpic if necessary.

function randstr($length) {
    $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $chooselength = strlen($chars);
    $string = '';
    for ($i = 0; $i < ((int) $length); $i++) {
        $string .= $chars[mt_rand() % $chooselength];
    }
}


What about uniqid ? It is not what you really asked, but according to the examples you gave, it could be what you're looking for

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜