Guid/Uuid in PHP with MySQL - php

I'd like to store uuids in the database as BINARY(16) but I need to accept them and present them as the XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format. Before I jump to split the guid in PHP code (what a fun that is!), are there any libraries or functions that do this out-of-the-box?

You can first remove the dashes with Replace()
and then use Unhex()Performs the inverse operation of HEX(str). That is, it interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number.

Don't you mean BINARY(36) instead of BINARY(16)
Try http://pecl.php.net/package/uuid
CakePHP has some neat functionality for uuids and it works automatically for CHAR(36) and BINARY(36) from what I understand. Either way you could dig into the source a bit to see if it has what you need.
Edit: Here is a stand alone class that can generate and convert into just about any format you might need: http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/ I posted this in the below comment but wanted to make sure the link is easily visible for other users.

Related

Is it possible to convert a full utf-8 url to a unique ID

Is it possible to convert a path utf-8 in url to a short and fixed unique ID in php?
I want every time,same result.
for example this:
questions/صصثبصث/ask?q=asd$a=سیبث
to be something like this: 34Sdd6N
CRC32 is the most suitable option when we talk about performance. If you use MySQL/MariaDB as a DBMS, you may create INT column with UNSIGNED flag and it'll store CRC32 result fully in 4 bytes only per each url.
PHP has crc32 function which accepts strings and has a very good dispersion of hashes, so you should not really care about duplicates.
You may also add dechex wrapper around your CRC32 result if you want to add ABCDEF letters to hash without database performance impact (and for sure use hexdec to convert it back).
save url in the database table and add column "code" or other name like it to table . you can add random string for every insert url and so get url with this code . be carfull do not using duplicate code .
What you are trying to ask is basically Hashing. There are some function available for hashing in php also. One of such functions is available here. I hope you will find it useful.

Is there a php port of namick/obfuscate_id rails plugin ?

Please check this
https://github.com/namick/obfuscate_id
This plugin converts id 7000 to 5270192353
I tried https://github.com/ivanakimov/hashids.php and it similar ones but it converts ids into a mix of alphabets like (yJJpo90) and numbers.I don't want that.I want IDs to convert into a positive integers.Are there any php packages for this sort?
You can try Optimus id transformation:
With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.
https://github.com/jenssegers/optimus

facebook userid - bigint mysql value

We are currently using bigint(20) to store userids from facebook.
However we are having some issues with the new userid format facebook is using:
Ex: 10152620408662937
The problem here is that mysql (and php) will rewrite the above number to:
1.0152620408663E+16
As you can imagine, our databases -bigint(20)- is having a hard time working with this.
Does anyone know what can be done about this?
Do we need to change the format from bigint(20) to something else, or perhaps parse the id in another way before storing it ?
Thank you
Given that:
These IDs don't actually have a numeric meaning
PHP lacks good support for large width integers
These values are not amenable to, nor do the operations you're performing on them require, the properties of floating point formats
You should store/manipulate them as strings.
You can use the GMP library in PHP:
$bigint = gmp_init("1.0152620408663E+16");
$bigint_string = gmp_strval(bigint);
var_dump($bigint_string);
//should output:
string(19) "10152620408662937"
GMP Library: http://php.net/manual/en/book.gmp.php

Cassandra columns order by UTF8 encoded string

I'm not sure if this is specific question for Cassandra or this can also belong to PHP so I'm sorry for tagging PHP.
So basically i'm ordering some long row columns by their column names, which goes like this:
2012-01-01_aa_99999 | 2012-01-01_aaa | 2012-01-12_aaaaa
So this is working the way i want it to work, but i don't understand how does it actually order those string.
What is not clear to me is that first string 2012-01-01_aa_99999 seems to be way bigger then the rest two, and i'm concerned that at some point it might ignore first part of the string which is a date and put some string where they shouldn't belong.
In my case those string consist of quite a few parts so i'm really concerned about this, so basically i need some explanation how does this ordering happens internally.
i don't understand how does it actually order those string.
The strings you provided appear to be lexicographically ordered.
I had the same question as I want to construct a composite primary key index with well-understood sorting abilities. It turns out Cassandra appears to compare UTF-8 strings using a byte-by-byte binary comparison... this is indeed a completely broken sort function from a logical perspective. If you had mixed ASCII and Kanji characters in your string, for example, your sort order would be effectively random. However, as long as this sort order is known, one can design your usage patterns around it.
This could be easily fixed, of course, and it would be nearly a single-line change of code to patch in a "real" sort function. This would require a bit extra CPU time, of course.

Is it safe to turn a UUID into a short code? (only use first 8 chars)

We use UUIDs for our primary keys in our db (generated by php, stored in mysql). The problem is that when someone wants to edit something or view their profile, they have this huge, scary, ugly uuid string at the end of the url. (edit?id=.....)
Would it be safe (read: still unique) if we only used the first 8 characters, everything before the first hyphen?
If it is NOT safe, is there some way to translate it into something else shorter for use in the url that could be translated back into the hex to use as a lookup? I know that I can base64 encode it to bring it down to 22 characters, but is there something even shorter?
EDIT
I have read this question and it said to use base64. again, anything shorter?
Shortening the UUID increases the probability of a collision. You can do it, but it's a bad idea. Using only 8 characters means just 4 bytes of data, so you'd expect a collision once you have about 2^16 IDs - far from ideal.
Your best option is to take the raw bytes of the UUID (not the hex representation) and encode it using base64. Or, just don't worry much, because I seriously doubt your users care what's in the URL.
Don't cut a single bit out of that UUID: You have no control over the algorithm that produced it, there are multiple possible implementation, algorithm implementation is subject to change (example: changed with the version of PHP you're using)
If you ask me an UUID in the address bar doesn't look scary or difficult at all, even a simple google search for "UUID" produces worst looking URL's, and everybody's used to looking at google URL's!
If you want nicer looking URL's, take a look at the address bar of this stackoverflow.com article. They're using the article ID followed by the title of the question. Only the ID part is relevant, everything else is there to make it easy on the eyes of readers (go ahead and try it, you can delete anything after the ID, you can replace it with junk - doesn't matter).
It is not safe to truncate uuid's. Also, they are designed to be globally unique, so you aren't going to have luck shortening them. Your best bet is to either assign each user a unique number, or let users pick a custom (unique) string (like a username, or nick name) that can be decoded. So you could have edit?id=.... or edit?name=blah and you then decode name into the uuid in your script.
It depends on how you're generating the UUID - if you're using PHP's uniqid then it's the right-most digits that are more "unique". However, if you're going to truncate the data, then there's no real guarantee that it'll be unique anyway.
Irrespective, I'd say that this is a somewhat sub-optimal approach - is there no way you can use a unique (and ideally meaningful) textual reference string instead of an ID in the query string? (Hard to know without more knowledge of the problem domain, but it's always a better approach in my opinion, even if SEO, etc. isn't a factor.)
If you were using this approach, you could also let MySQL generate the unique IDs, which is probably a considerably more sane approach than attempting to handle this in PHP.
If you're worried about scaring users with the UUID in the URL, why not write it out to a hidden form field instead?

Categories