Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created September 19, 2011 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrclay/1225832 to your computer and use it in GitHub Desktop.
Save mrclay/1225832 to your computer and use it in GitHub Desktop.
RotUrl: encode/obfuscate URLs for use in other URLs
<?php
/**
* Rot35 for URLs. To avoid increasing size during urlencode(), commonly encoded
* chars are mapped to more rarely used chars (end of the uppercase alpha).
*
* @param string $url
* @return string
*/
function rotUrl($url) {
return strtr($url,
'./-:?=&%# ZQXJKVWPY abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHILMNORSTU',
'ZQXJKVWPY ./-:?=&%# 123456789ABCDEFGHILMNORSTUabcdefghijklmnopqrstuvwxyz');
}
@gaabora
Copy link

gaabora commented Oct 18, 2019

It breaks the url if source url contains any of ZQXJKVWPY.

@mrclay
Copy link
Author

mrclay commented Oct 18, 2019

If you're using urlencode() correctly it does not.

@gaabora
Copy link

gaabora commented Oct 18, 2019

Then how about

if (! function_exists('rotUrl')) {
    function rotUrl($url) {
        return urlencode(strtr($url,
            './-:?=&%# ZQXJKVWPY abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHILMNORSTU',
            'ZQXJKVWPY ./-:?=&%# 123456789ABCDEFGHILMNORSTUabcdefghijklmnopqrstuvwxyz'));
    }
}
if (! function_exists('unrotUrl')) {
    function unrotUrl($url) {
        return strtr(urldecode($url),
            './-:?=&%# ZQXJKVWPY abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHILMNORSTU',
            'ZQXJKVWPY ./-:?=&%# 123456789ABCDEFGHILMNORSTUabcdefghijklmnopqrstuvwxyz');
    }
}

@mrclay
Copy link
Author

mrclay commented Oct 18, 2019

urlencode() belongs in your code to properly encode query values in URLs. PHP's $_GET will then have the correct value to feed back into rotUrl().

@mrclay
Copy link
Author

mrclay commented Oct 18, 2019

Your app must have proper query string encoding and HTML attribute escaping of the encoded URL. Otherwise you'll have vulnerabilities, not just problems transmitting values.

@gaabora
Copy link

gaabora commented Oct 18, 2019

We must save this conversation here as a future usage note tho.
So for safe usage

urlencode(rotUrl($url)) // to encode 
rotUrl(urldecode($url)) // to decode 

http://codepad.org/rz0faInK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment