9

在使用像 JSFiddle 这样的网站后,我注意到它们会自动生成一个由各种大小写字符组成的随机且唯一的 URL。

我们可以从预订页面中受益。它是如何完成的?

4

5 回答 5

20

这不是随机的,它基于您的数据库记录的 ID。

这个怎么运作:

基本上你有一个唯一的字符串,但它可以被解密来代表一个数字,你应该把它看作是一个简短的加密/解密。

您有一个函数,该函数将采用唯一 ID,然后从该 ID 创建一个唯一的“短字符串”,然后您可以反转该过程以从短字符串中获取唯一 ID。

这是我在一个网站上找到的片段:

function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
{
    $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if ($passKey !== null)
    {
        /* Although this function's purpose is to just make the
        * ID short - and not so much secure,
        * with this patch by Simon Franz (http://blog.snaky.org/)
        * you can optionally supply a password to make it harder
        * to calculate the corresponding numeric ID */

        for ($n = 0; $n<strlen($index); $n++)
        {
            $i[] = substr( $index,$n ,1);
        }

        $passhash = hash('sha256',$passKey);

        $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash;

        for ($n=0; $n < strlen($index); $n++)
        {
            $p[] =  substr($passhash, $n ,1);
        }

        array_multisort($p,  SORT_DESC, $i);
        $index = implode($i);
    }

    $base  = strlen($index);

    if ($to_num)
    {
        // Digital number  <<--  alphabet letter code
        $in  = strrev($in);
        $out = 0;
        $len = strlen($in) - 1;

        for ($t = 0; $t <= $len; $t++)
        {
            $bcpow = bcpow($base, $len - $t);
            $out   = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
        }

        if (is_numeric($pad_up))
        {
            $pad_up--;
            if ($pad_up > 0)
            {
                $out -= pow($base, $pad_up);
            }
        }
        $out = sprintf('%F', $out);
        $out = substr($out, 0, strpos($out, '.'));
    }
    else
    {
        // Digital number  -->>  alphabet letter code
        if (is_numeric($pad_up))
        {
            $pad_up--;
            if ($pad_up > 0)
            {
                $in += pow($base, $pad_up);
            }
        }

        $out = "";
        for ($t = floor(log($in, $base)); $t >= 0; $t--)
        {
            $bcp = bcpow($base, $t);
            $a   = floor($in / $bcp) % $base;
            $out = $out . substr($index, $a, 1);
            $in  = $in - ($a * $bcp);
        }
        $out = strrev($out); // reverse
    }
    return $out;
}

例子

alphaID(9007199254740989);   //-> PpQXn7COf
alphaID('PpQXn7COf', true);  //-> 9007199254740989

这里有一个脚本链接:https ://github.com/kvz/deprecated/blob/kvzlib/php/functions/alphaID.inc.php

于 2011-03-24T16:19:17.910 回答
2

如果您只需要指定长度的随机 id,请使用uniqid()函数;如果您需要可重复的内容,请使用长 url的md5()哈希。(对于给定的输入,输出总是相同的)。使用 PHP 和 MySQL 创建短 URL 服务很好地说明了如何将它们组合在一起。

于 2011-03-24T16:08:38.187 回答
1

这很简单。

您的网址类似于 www.domain.com/in/here/RANDOMPART

您将 www.domain.com/in/here/* 重写为您选择的脚本。在此脚本中,您可以使用全局变量获取请求的 uri,您可以将其拆分并使用最后一个“/”之后的部分。这是你的随机部分。yust 对照你的数据库或其他东西检查它。

要创建这样的 url,您只需在“www.domain.com/in/here/”后面添加一些东西。您可以使用md5(uniqid(rand(), true)).

不要忘记将此字符串保存在数据库或其他任何内容中。

希望,这有帮助。

于 2011-03-24T16:14:58.420 回答
1

这些方面的东西:

<?php
  $url_length = rand(10,20); //generate a random number between 10 and 20 for the length   of the URL
  $url = array($url_length);
  for($i=0; $i < $url_length; $i++)
  {
     $char = 0;
     while($char < 65 OR $char > 122 OR ($char > 91 AND $char < 97))
     {
       $char = rand(65,122);
     }
     $url[] = chr($char);
  }
  print_r($url);
?>

请注意,这是部分伪代码。这将创建一个具有随机长度(介于 10 到 20 之间)的 URL,并使用与字母表中的字母对应的随机 ASCII 代码填充每个字符。ASCII 表中的大小写字母之间有一些“垃圾”字符,因此这将重新滚动一个随机数,直到生成一个有效的 ASCII 数。

这假设您将此 URL 存储在数据库中。为确保此字符串是唯一的,您需要检查它是否存在于数据库中。如果没有,那么添加它就可以了。如果存在,则再次重做该过程,直到获得唯一的字符串。

于 2011-03-24T16:15:09.767 回答
0

您可以简单地创建一个包含所有潜在字符的数组,然后随机挑选出您想要的任意数量并将它们作为 URL“代码”返回?

$arr = array('a','b','c','d');
$count = count($arr) - 1;
$str = $arr[rand(0, $coun)].$arr[rand(0, $coun)];
于 2011-03-24T16:07:58.037 回答