CDN 分发的 PHP 算法

发布于 2024-09-13 08:52:14 字数 1927 浏览 5 评论 0原文

忽略问题:

我在其他文件中包含的 CSS 文件因此具有相关性 *facepalm*


我们有以下代码用于为每个文件名选择 CNAME CDN 引用。它必须每次根据给定的文件名返回相同的 URL。我们认为这足够随机:

<?php

  function cdn_prefix($fileName) {
    $number_of_servers = 4;

    $md5 = md5($fileName);

    $md5 = substr($md5, 0, 4);

    $hash_number = base_convert($md5, 16, 10);

    $server_number = ($hash_number % $number_of_servers) + 1;

    $server_prefix = '//static' . $server_number . '.' . $_SERVER['SERVER_NAME'];

    return $server_prefix . $fileName;
  }

?>

然而,它似乎有利于数字 3:MD5

无论我做什么(盐、不同的碱基、随机乘法等),结果 headerBg 到 mainNavPipe (在屏幕截图上)都具有相同的数字。

有更好的算法吗?

编辑:

以下是使用 SHA1 的相同算法的结果 SHA1

任何地方都调用相同的函数 - 因为它返回整个 URL,并且不会显示 static[1-4] 域,除非通过此函数。

数组(用于测试)是:

FILES = [
    '/a/files/image/250.jpg',
    '/a/files/image/244.jpg',
    '/a/files/image/247.jpg',
    '/a/css/global/core.css',
    '/a/css/global/print.css',
    '/a/img/global/new_logo.gif',
    '/a/img/global/book-a-free-survey.gif',
    '/a/img/global/make_an_enquiry.gif',
    '/a/img/global/purchase-locks-blue.jpg',
    '/a/files/image/251.jpg',
    '/a/img/global/bg.gif',
    '/a/img/global/headerBg.jpg',
    '/a/img/global/basketBg.gif',
    '/a/img/global/arrow.png',
    '/a/img/global/trolley.gif',
    '/a/img/global/mainNavBg.gif',
    '/a/img/global/mainNavCurrentBg.gif',
    '/a/img/global/mainNavPipe.gif',
    '/a/img/common/sectionNavBg.jpg',
    '/a/img/global/nav_arrow.gif',
    '/a/img/global/footerBg.jpg',
    '/a/img/global/footerCopyrightBg.jpg',
    '/a/img/global/footerLogo.jpg'
]

IGNORE THE QUESTION:

The CSS File I was including pulled in the the other files hence the correlation *facepalm*


We have the following code for picking a CNAME CDN reference per filename. It must return the same URL everytime based on a given filename. We thought this would be sufficiently random:

<?php

  function cdn_prefix($fileName) {
    $number_of_servers = 4;

    $md5 = md5($fileName);

    $md5 = substr($md5, 0, 4);

    $hash_number = base_convert($md5, 16, 10);

    $server_number = ($hash_number % $number_of_servers) + 1;

    $server_prefix = '//static' . $server_number . '.' . $_SERVER['SERVER_NAME'];

    return $server_prefix . $fileName;
  }

?>

However it seems to favour the number 3:MD5

No matter what I do (salt, different bases, random multiplication, etc) the results headerBg through to mainNavPipe (on the screen shot) all have the same number.

Is there a better algorithm?

EDIT:

Here are the results using same algorithm using a SHA1
SHA1

Everywhere calls the same function - as it returns the whole URL and wouldn't show the static[1-4] domain unless it when through this function.

The array (for testing) is:

FILES = [
    '/a/files/image/250.jpg',
    '/a/files/image/244.jpg',
    '/a/files/image/247.jpg',
    '/a/css/global/core.css',
    '/a/css/global/print.css',
    '/a/img/global/new_logo.gif',
    '/a/img/global/book-a-free-survey.gif',
    '/a/img/global/make_an_enquiry.gif',
    '/a/img/global/purchase-locks-blue.jpg',
    '/a/files/image/251.jpg',
    '/a/img/global/bg.gif',
    '/a/img/global/headerBg.jpg',
    '/a/img/global/basketBg.gif',
    '/a/img/global/arrow.png',
    '/a/img/global/trolley.gif',
    '/a/img/global/mainNavBg.gif',
    '/a/img/global/mainNavCurrentBg.gif',
    '/a/img/global/mainNavPipe.gif',
    '/a/img/common/sectionNavBg.jpg',
    '/a/img/global/nav_arrow.gif',
    '/a/img/global/footerBg.jpg',
    '/a/img/global/footerCopyrightBg.jpg',
    '/a/img/global/footerLogo.jpg'
]

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

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

发布评论

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

评论(1

勿挽旧人 2024-09-20 08:52:14

这可能是一次性的事情或其他地方的错误。

function cdn_prefix($fileName) {
    $number_of_servers = 4;
    $md5 = md5($fileName);
    $md5 = substr($md5, 0, 4);
    $hash_number = base_convert($md5, 16, 10);
    $server_number = ($hash_number % $number_of_servers) + 1;
    return $server_number;
}
$arr = array(1=>0, 2=>0, 3=>0, 4=>0,);
for ($i = 1; $i < 200000; $i++) {
    $arr[cdn_prefix("anrg".$i)]++;
}
print_r($arr);

给出:

Array
(
    [1] => 49770
    [2] => 50090
    [3] => 50026
    [4] => 50113
)

This was probably a one-time thing or a bug elsewhere.

function cdn_prefix($fileName) {
    $number_of_servers = 4;
    $md5 = md5($fileName);
    $md5 = substr($md5, 0, 4);
    $hash_number = base_convert($md5, 16, 10);
    $server_number = ($hash_number % $number_of_servers) + 1;
    return $server_number;
}
$arr = array(1=>0, 2=>0, 3=>0, 4=>0,);
for ($i = 1; $i < 200000; $i++) {
    $arr[cdn_prefix("anrg".$i)]++;
}
print_r($arr);

gives:

Array
(
    [1] => 49770
    [2] => 50090
    [3] => 50026
    [4] => 50113
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文