1

现在我可以通过简单地在 URL 中使用它的 ID 来获取数据库中产品的信息,如下所示:http://www.example.com/?productID=5
现在我可以猜测下一个,因为这些 ID 都是自动递增的。
您将如何隐藏此 ID 并将其替换为更好的 ID?我的意思是它仍然必须是一个唯一的标识符。
为了使它复杂一点,我想我可以使用全局唯一标识符并将其添加到数据库中的另一列中。但我觉得这不是最好的方法:)
请让我知道你的想法,并告诉我你是如何解决/将在你的项目中解决这个问题的。

我基本上想要的是摆脱知道下一个/上一个 ID。

产品本身没有安全限制。用户很可能被允许看到它们。我在想这种技术可以防止抓取所有产品?!?

就像 amazon.com 所做的那样:看起来他们以某种方式隐藏了商品的 ID,对吧?

4

4 回答 4

2

你也可以使用一个非常适合 SEO 的 slug。所以产品 1 slug 可能是http://www.example.com/?productSlug=exampleitemnameasoneword 您只需要确保 slug 是唯一的。您当然需要在数据库条目中添加一个 slug 字段并能够输入/编辑它。

于 2012-12-04T15:37:32.553 回答
1

首先,看看mod_rewrite

我正在使用CakePhp,这使我可以轻松访问由.htaccess规则创建的 url。就我而言,我的标题是 100% 唯一的,这使我可以拥有标题的 slug 版本。

比如我的标题是SZD-51 Junior,这会变成SZD-51_Junior。我只是将它添加slug到我的数据库中并将其用作辅助唯一标识符。当用户访问时www.example.com/planes/SZD-51_Junior,我可以执行以下操作:

// pseudo mysql
SELECT *
FROM table
WHERE slug = url_param

//in cakephp
$this->Plane->findBySlug($this->params['slug']);

有很多方法,你希望url变成什么?哪种格式?

于 2012-12-04T15:39:28.187 回答
1

您可以使用某种散列算法,例如 md5()、sha1() 等……是的,最好将该散列存储在表的另一列中以便更快地查找。

如果您使用 md5() ,最好使用固定或随机的盐字符串播种它。我最喜欢的是用 microtime() 来做。

在数据库插入之前生成哈希:

$salt = "my_secret_salt_string";
$entry['hash_code'] = md5($salt . microtime()); // every time a different result is generated which will ensure uniqueness
// insert into ...

或者,如果您更喜欢使用生成的 id 作为盐,则更新条目:

// insert into ...
// get id
$entry['hash_code'] = md5($id . microtime());
// update ...
于 2012-12-04T15:41:29.813 回答
-2

一个解决方案是使用 post 代替,这只是我做的一个快速混搭。

<?php

//how to handle the request
if(isset($_POST['transfer']) && !empty($_POST['transfer']))
{
    $transfer = $_POST['transfer'];

    //what to do?
}

?>

//how to make a link with id 5
<a href="javascript:transfer('5');">link</a>

//this is for the functionality
<form id="transfer_form" action="http://www.example.com/" method="POST">
    <input type="hidden" id="transfer" name="productID" value="" />
</form>

//this should be placed within the <head>
<script>
function tranfer(link)
{
    document.getElementById('transfer').value = link;
    document.getElementById('transfer_form').submit();
}
</script>
于 2012-12-04T15:34:00.440 回答