views:

549

answers:

4

Hello all,

I wish to be able to generate URL variables like this:

http://example.com/195yq
http://example.com/195yp
http://example.com/195yg
http://example.com/195yf

The variables will match to a MySQL record set so that I can pull it out. At the time of creation of that record set, I wish to create this key for it.

How can I do this? What is the best way to do this? Are there any existing classes that I can make use of?

Thanks all

A: 

This can be done pretty straightforward in a couple of ways:

  • Use a hash, such as MD5. (would be long)
  • Base-64 encode a particular ID or piece of data

If you're asking whether or not there is anything that does this already with MySQL records, I'm not sure that you'll find something as design-wise, data is really, really conceptually far away from the URLs in your website, even in frameworks like Grails. Most don't even attempt to wrap up front-end and data-access functionality into a single piece.

altCognito
Base-64 is long itself. 4 Characters turn into 8, right?
Abs
True, very true :)
altCognito
A: 

In what context will you be using this? Why not just pass post variables? It is far more secure and neat. You will still accomplish the number in the url such as id=195yq, and there is a way to hide them by configuring your php.ini file.

I hope this can be of help to you.

Please keep this in mind. When you pass variables in the address bar it is easy for someone to change the variable, and access information you may not want them to access.

Chris B.
These URLs can be used by anyone to find a particular page on my site. There will be many pages and content will be user generated. It needs to be as short as possible.
Abs
I understand. Sorry, that I cannot help you with. Best of luck though, this will be very helpful to me as well so I will be watching. +1 for a good question!
Chris B.
A: 

In the examples you gave, it looks like you're base-64 encoding the numeric primary key. That's how I would do it and have done it in the past, but I'm not sure it's any better than passing the ID in the clear because base-64 decoding is trivial.

Scott
Do you mean the usage of base64_encode or something else? As this will result in a variable with a lot of characters. I am not sure how unique it can be either?
Abs
Yes, I used the base64_encode() function. My database grows very slowly so the encoded ID is not very long yet.
Scott
A: 

Basically, you need a hash function of some sort.

This can be SHA-1 function, an MD5 function (as altCogito), or using other data you have in the record and encoding it.

How many records do you think you will have? Be careful on selecting the solution as a hash function has to be big enough to cover you well, but too big and you create a larger database than you need. I've used SHA-1 and truncate to 64 or 32 bits, depending on need.

Also, look at REST. Consider that the URL may not have to be so mysterious...

alphadogg
I am hoping for records to be in the millions at the very least. Optimistic, yes. :) I would rather show this unique variable to users rather than my primary auto increment id not because I am hiding content but because I don't want it to be too long.
Abs
I agree that sometimes URLs are lengthy affairs. But, there's something to be said for a human-readable URL that tells you what resource your are going to get. Most people do not type in URLs, but rather hit links or use Google. I can't remember the last time I actually typed in a full URL where I wish someone had given me a TinyURL for it...
alphadogg
Hmm, Good point, I actually can't imagine that too.
Abs