PHP 随机 URL 名称(短 URL)

发布于 2024-10-25 12:08:01 字数 90 浏览 3 评论 0原文

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

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

After using sites like JSFiddle I noticed that they auto generate a random and unique URL made up of various upper and lower case characters.

We could benefit from this for our booking pages. How is it done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瀟灑尐姊 2024-11-01 12:08:01

这不是随机的,而是基于数据库记录的 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

This is not random, there based of the ID of your database record.

How it works:

basically you have a string that is unique but it can be decrypted to represent a number, you should look at it as a short encryption / decryption.

You have a function that would take an unique ID and then it creates a Unique 'short string' from that id, and then you can reverse the process to get the unique id from the short string.

Here's a snipped I have found of a website:

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;
}

Example

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

there's a link to the script here: https://github.com/kvz/deprecated/blob/kvzlib/php/functions/alphaID.inc.php

巨坚强 2024-11-01 12:08:01

如果您只想要指定的随机 ID,请使用 uniqid() 函数长度或使用长网址的 md5() 哈希(如果您需要一些可重复的东西。 (对于给定的输入,输出始终相同)。 使用 PHP 和 MySQL 创建短 URL 服务 很好地概括了如何将它们组合在一起。

Use the uniqid() function if you just want a random id of a specified length or use the md5() hash of your long url if you need something repeatable. (output always the same for a given input). Creating a short URL service using PHP and MySQL gives a good rundown of how to put it all together.

梦途 2024-11-01 12:08:01

沿着这些思路:

<?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 存储在数据库中。为了确保该字符串是唯一的,您需要检查它是否存在于数据库中。如果没有,那么添加它就可以了。如果存在,则再次重复该过程,直到获得唯一的字符串。

Something along these lines:

<?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);
?>

Note this is partially psuedo code. This creates a URL with a random length (somewhere between 10 and 20), and populates each character with a random ASCII code that cooresponds to a letter in the alphabet. There are a few "junk" characters between the upper and lower case letters in the ASCII table, so this will reroll a random number until a valid ASCII number is generated.

This assumes you are storing this URL in a database. To ensure that this string is unique, you need to check to see if it exists yet in the database. If not, then add it and you're fine. If it exists, then redo the process again until you get a unique string.

一花一树开 2024-11-01 12:08:01

这很简单。

您的 URL 类似于:

www.example.com/in/here/RANDOMPART

您重写 www.example.com/in/here/* > 到您选择的脚本。在此脚本中,您可以使用全局变量获取请求的 URI,并且可以将其拆分并使用最后一个 / 之后的部分。这是您的RANDOMPART。你需要根据你的数据库或者其他什么来检查它。

要创建这样的 URL,您只需在 www.example.com/in/here/ 后面添加一些内容即可。您可以使用 md5(uniqid(rand(), true)) 生成一个漂亮且希望唯一的字符串。

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

我希望这有帮助。

It is very simple.

Your URL is something like:

www.example.com/in/here/RANDOMPART

You rewrite www.example.com/in/here/* to a script of your choice. In this script you can get the requested URI with the globals and you can split it and use the part after the last /. This is your RANDOMPART. yust check it against your Database or whatever.

To create such an URL you simply have to add something behind www.example.com/in/here/. You could generate a nice hopefully unique string with md5(uniqid(rand(), true)).

Do not forget to save this string in a Database or whatever.

I hope that helps.

月下客 2024-11-01 12:08:01

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

$arr = array('a','b','c','d');
$count = count($arr) - 1;
$str = $arr[rand(0, $coun)].$arr[rand(0, $coun)];

You could simply create an array of all potential characters then randomly pick out however many you want and return them as the URL "code"?

$arr = array('a','b','c','d');
$count = count($arr) - 1;
$str = $arr[rand(0, $coun)].$arr[rand(0, $coun)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文