我假设您(无论出于何种原因)试图阻止某人访问类似的页面
profile/<user_id>,看到user_id = 100,然后尝试profile/101、profile/102等。
如果是这种情况,那么您可以使用类似的东西(以及自动递增的 id)
class Crypt {
public static function encrypt($data) {
$config = LoadSomeConfig();
// open the module to be used. There are several listed at http://www.php.net/manual/en/mcrypt.ciphers.php
$mod = mcrypt_module_open($config->cipher, '', $config->mode, '');
// use config set initialization vector. We will use a constant here as we do not want to include this for decryption
if (isset($config->vector)) {
$iv = $config->vector;
} else {
die("NO IV SET!");
}
$key_size = mcrypt_enc_get_key_size($mod);
$key = substr($config->key, 0, $key_size);
mcrypt_generic_init($mod, $key, $iv);
// Do the encryption using the cipher module defined
$encrypted = mcrypt_generic($mod, $data);
// cleanup
mcrypt_generic_deinit($mod);
mcrypt_module_close($mod);
// Changed the output based on the config encoding value. Currently supported values, base64 and hex.
switch ($config->encoding) {
case "base64":
$encrypted = base64_encode($encrypted);
break;
case "hex":
$encrypted = bin2hex($encrypted);
break;
default:
break;
}
return $encrypted;
}
public static function decrypt($data){
if (empty($data)) {
return '';
}
// config options set include the cipher, mode and secret key
$config = LoadSomeConfig();
// Change encrypted data base to binary based on the encoding mechanism used to generate the data
switch ($config->encoding) {
case "base64":
$data = base64_decode($data);
break;
case "hex":
$data = pack("H*", $data);
break;
default:
break;
}
if (isset($config->vector)) {
$iv = $config->vector;
} else {
die("NO IV SET!");
}
$mod = mcrypt_module_open($config->cipher, '', $config->mode, '');
$key_size = mcrypt_enc_get_key_size($mod);
// max key size is 448 bits
$key = substr($config->key, 0, $key_size);
mcrypt_generic_init($mod, $key, $iv);
// decrypt the data
$decrypted = mdecrypt_generic($mod, $data);
// cleanup
mcrypt_generic_deinit($mod);
mcrypt_module_close($mod);
return trim($decrypted);
}
}
然后你会有一个类似profile/c2ffd340ea3b71ca065e6add4143f36d的路由
在您的个人资料页面中,假设在 user_id 中可以访问 user_id,您可以简单地执行以下操作:
$user_id = Crypt::decrypt($user_id);
然后照常进行。在创建指向某人的个人资料页面的链接时,您可以使用 profile/<?php echo Crypt::encrypt($user->user_id); ?>