Problem: I am using Opaque class to encode/decode IDs which works great, however after months when I submitted sitemaps to Google I realized that this class add a SLASH in IDs e.g. 173557 is converted to /eP/2Q ... now this has 2 slashes and when I open the url it works fine but Google Search Console gives me 404 error on this page, how can I fix this code not to include a . and a slash / in the encoded ID. Secondly, in Bing Webmaster Tools, all urls are small letters, but here I have Capital letter, Bing also gives 404 on thousands of urls which load fine for me.
Sorry for this question, I tried removing the '+/' from the code to fix my problem which didn't work. So, I had to ask if someone could help.
PHP Class on github: https://github.com/marekweb/opaque-id
Below is the code:
/**
* 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]);
}
}
I added the folloding in my functions.php
$encoder = new OpaqueEncoder(0x3b79db9a, OpaqueEncoder::ENCODING_BASE64);
For decoding I use:
$decoded_id = $encoder->decode($id);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.