添加斜杠的PHP ID编码器
问题:我正在使用不透明类对ID进行编码/解码,这非常有效,但几个月后,当我向谷歌提交网站地图时,我意识到该类在ID中添加了斜杠,例如173557被转换为/eP/2Q。。。现在这有2个斜杠,当我打开url时,它工作正常,但谷歌搜索控制台在这个页面上给了我404错误,我如何修复这个代码不包括一个。第二,在Bing网站管理员工具中,所有的URL都是小写字母,但这里我有大写字母,Bing也给出了404对数千个URL的加载 很抱歉出现这个问题,我尝试从代码中删除“+/”,以解决我的问题,但没有成功。所以,我不得不问是否有人能帮忙 github上的PHP类: 代码如下:添加斜杠的PHP ID编码器,php,Php,问题:我正在使用不透明类对ID进行编码/解码,这非常有效,但几个月后,当我向谷歌提交网站地图时,我意识到该类在ID中添加了斜杠,例如173557被转换为/eP/2Q。。。现在这有2个斜杠,当我打开url时,它工作正常,但谷歌搜索控制台在这个页面上给了我404错误,我如何修复这个代码不包括一个。第二,在Bing网站管理员工具中,所有的URL都是小写字母,但这里我有大写字母,Bing也给出了404对数千个URL的加载 很抱歉出现这个问题,我尝试从代码中删除“+/”,以解决我的问题,但没有成功。所以,
/**
* Opaque ID encoder.
*
* Translates between 32-bit integers (such as resource IDs) and obfuscated
* scrambled values, as a one-to-one mapping. Supports hex and base64 url-safe
* string representations. Expects a secret integer key in the constructor.
*
* (c) 2011 Marek Z. @marekweb
*/
class OpaqueEncoder {
private $key;
private $extraChars = '.';
const ENCODING_INT = 0;
const ENCODING_HEX = 1;
const ENCODING_BASE64 = 2;
/**
* @param $key Secret key used for lightweight encryption.
*/
public function __construct($key, $encoding = self::ENCODING_HEX) {
$this->key = $key;
$this->encoding = $encoding;
}
/**
* Produce an integer hash of a 16-bit integer, returning a transformed 16-bit integer.
*/
protected function transform($i) {
$i = ($this->key ^ $i) * 0x9e3b;
return $i >> ($i & 0xf) & 0xffff;
}
/**
* Reversibly transcode a 32-bit integer to a scrambled form, returning a new 32-bit integer.
*/
public function transcode($i) {
$r = $i & 0xffff;
$l = $i >> 16 & 0xffff ^ $this->transform($r);
return (($r ^ $this->transform($l)) << 16) + $l;
}
/**
* Encode a value according to the encoding mode selected upon instantiation.
*/
public function encode($i) {
switch ($this->encoding) {
case self::ENCODING_INT:
return $this->transcode($i);
case self::ENCODING_BASE64:
return $this->encodeBase64($i);
case self::ENCODING_HEX:
default:
return $this->encodeHex($i);
}
}
/**
* Decode a value according to the encoding mode selected upon instantiation.
*/
public function decode($s) {
switch ($this->encoding) {
case self::ENCODING_INT:
return $this->transcode($s);
case self::ENCODING_BASE64:
return $this->decodeBase64($s);
case self::ENCODING_HEX:
default:
return $this->decodeHex($s);
}
}
/**
* Transcode an integer and return it as an 8-character hex string.
*/
public function encodeHex($i) {
return dechex($this->transcode($i));
}
/**
* Transcode an integer and return it as a 6-character base64 string.
*/
public function encodeBase64($i) {
return strtr(substr(base64_encode(pack('N', $this->transcode($i))), 0, 6), '+/', $this->extraChars);
}
/**
* Decode an 8-character hex string, returning the original integer.
*/
public function decodeHex($s) {
return $this->transcode(hexdec($s));
}
/**
* Decode a 6-character base64 string, returning the original integer.
*/
public function decodeBase64($s) {
$unpacked = unpack('N', base64_decode(strtr($s, $this->extraChars, '+/')));
return $this->transcode($unpacked[1]);
}
}
对于解码,我使用:
$decoded_id = $encoder->decode($id);
当我用print$encoder->encode(173557)运行这个时代码>我得到的6l9FLg
没有斜杠。你确定你的整数是32位的吗?是否打印PHP\u INT\u MAX代码>返回2147483647?
$decoded_id = $encoder->decode($id);