Friday, 15 March 2013

php - i want to give the user a random number the first time they come on my webpage -


I want to do this so that they can browse my content anonymously but still on syncing the information on other devises. capable.

There are several ways to get a unique ID in PHP, however, not all of them are as unique as You can think.

Rand

  $ userId = rand (1, 99999999); It's a lot worse to rely on, as the randomness is like throwing a dice, there are quite a good obstacles that will double it to a similar value.  

UniqId

  $ userId = uniqid ('prefix' is true);  

There is a better suggestion on handling the user's important data but still is not unsuccessful The second parameter provides you with some entropy

GUID

The most commonly used version for the unique ID is currently the GUID coming from the Microsoft world, it provides the most entropy. Many methods of PDAP are GUID

Hosting PHP implementation on Windows:

  $ userId = com_create_guid ( ); For example, getGUID () {if (function_exists ('com_create_guid'))    for example  

Or create your own implementation {return com_create_guid (); } Other {MT_SEND ((double) microgram () * 10000); // Optional for PHP 5.2.0 and up $ Charid = strtoupper (md5 (uniqid (rand (), true))); $ Hyphen = chr (45); // "-" $ uuid = chr (123) // "{" .substr ($ charid, 0, 8). $ Hyphen .substr ($ charid, 8, 4). $ Hyphen .substr ($ charid, 12, 4) $ Hyphen .substr ($ charid, 16, 4). $ Hyphen .substr ($ charid, 20,12). CHR (125); // "$" refund $ UUID; }}

ID sending and storage

User ID can be stored best in a session variable. It can be used depending on the php system:

  $ _ session ['userID'] = $ userId;  

and retrieve other requests such as AJAX requests for notification notifications (example turn the example of code around).

You can also choose to save cookies in public, you can create JS available prices.


No comments:

Post a Comment