homerun
homerun

Reputation: 20765

PHP: reverse able unique hash

I'm building a browser in PHP (browser in browser (: just for fun ) , and i have come across a major problem. I need to short a URL into something shorter (encoded) which can be reverse able.

for example now i process the page like that :

site.com/stackoverflow.com/questions/9217271/php-reverse-able-unique-hash

and that's too long , i was looking for something that will be like that :

site.com/sftDblt

which the "sftDblt" can be reverse able to "stackoverflow.com/questions/9217271/php-reverse-able-unique-hash"

is there anything you know about that can help?

Upvotes: 3

Views: 5221

Answers (6)

kislay kumar
kislay kumar

Reputation: 69

Take a look at base64_encode() and base64_decode().

Upvotes: 2

KevBurnsJr
KevBurnsJr

Reputation: 4878

I created a small class to do this sort of thing with the help of some commenters.
http://blog.kevburnsjr.com/php-unique-hash

Upvotes: -1

Rokhayakebe
Rokhayakebe

Reputation: 119

Try something like this: use the first letter of each word in the title and save it in a column in the post table.

Using your example (site.com/stackoverflow.com/questions/9217271/php-reverse-able-unique-hash), the url would be "prauh".

If the url happens to exists in the DB, you can add 2 random alphanumeric characters then check your DB again to see if it exists.

Upvotes: 0

A hash can't be reversed. You should go with http://php.net/manual/en/book.mcrypt.php

Upvotes: 2

Jonathan M
Jonathan M

Reputation: 17451

Why not just start a counter for the key? Keep track of the url and counter in a database.

Upvotes: 3

Matthew
Matthew

Reputation: 48284

What you're asking for is (basically) impossible without a lookup table. You can obviously use standard compression algorithms, but then you'd have to encode the compressed data as an ASCII string (base-64, etc), which would possibly be larger than the original string.

Upvotes: 2

Related Questions