Question

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

Was it helpful?

Solution

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...

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top