0

I want to do this so that they can anonymously browse my content but still be able to synchronise notifications on other devises.

  • I cant' see the link between the aim and the request... – Masiorama Mar 17 '15 at 08:35
  • Wow, then you need to implement Steve's Gibson SQRL, it is exactly what it is doing. It is in development stage, so you need to wait though... – n-dru Mar 17 '15 at 08:36

3 Answers3

1

There are several ways of coming to a uniqueID in PHP. However, not all of them as unique as you might think.

Rand

http://php.net/manual/en/function.rand.php

$userId = rand(1, 99999999);

This one is very bad to rely on, as the randomness is like throwing a dice. There are quite good odds that it will double a same value.

UniqId

http://php.net/manual/en/function.uniqid.php

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

A better suggestion but still not fail safe if handling user critical data. The second parameter provides you with some more entropy

GUID

The most used variants to unique id's is currently GUIDs. Coming from the Microsoft world it provides the most entropy. There are several ways of generation a GUID

On Windows hosted PHP implementations:

http://php.net/manual/en/function.com-create-guid.php

$userId = com_create_guid();

When you have access to PECL

Install http://pecl.php.net/package/uuid

$userId = uuid_create(); 

Or create an own implementation For example

function getGUID(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.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);// "}"
        return $uuid;
    }
}

Sending and storing the ID

The userId might best be stored in a Session variable. Depending on the PHP system used it might be something like:

$_SESSION['userID'] = $userId;

And retrieve in other requests (turn the code example around), like AJAX request for notification updates.

You could of course also opt to save in a public cookie, making the value available to JS.

mvbrakel
  • 936
  • 5
  • 16
  • Thanks a lot for your help. I will take a look at your links. – MMultiLinguist Mar 17 '15 at 09:41
  • @mvbrakel I just found out today that `uniqid()` can use a prefix, nice option. In seeing `$b = uniqid (about, true);` where I found it at http://phpgoogle.blogspot.ca/2007/08/four-ways-to-generate-unique-id-by-php.html - they didn't put quotes around `about` and it does work. However, when I tried to add a hyphen `$b = uniqid (about-, true);` that failed miserably, yet using an underscore `$b = uniqid (about_, true);` worked *strangely enough*. I Google'd and found your answer and added the hyphen in there and it worked beautifully. I just thought you'd like to know that, *cheers*. – Funk Forty Niner Dec 02 '15 at 19:34
  • @Fred, please look in your error log. The function is specified to require a string.. The reason your example works and does not work with a dash is because PHP sees an unquoted string as constant. If a constant does not have a value it tries to parse it into a string. Very bad practice though. – mvbrakel Dec 03 '15 at 20:47
0

You probably want to use the rand() function in php.

<?php
     echo rand();
?>

You can find more information on it here:

http://php.net/manual/en/function.rand.php

Then store it in your database or in a session.

Depending on whether or not you've used sessions before, there is a good tutorial here:

http://www.w3schools.com/php/php_sessions.asp

You could do something like this:

<?php
    // Start the session
    session_start();
    $random_no = rand(); 

   // Set session variables
   $_SESSION["visitor_number"] = $random_no;
?> 

The one problem with rand() is that depending on the number of visitors who visit your site, you could end up with duplicates. It would be worth storing the doing some checking to ensure that you don't end up with duplication.

This thread might help you with that:

Generating UNIQUE Random Numbers within a range - PHP

Hope that helps!

Community
  • 1
  • 1
hlh3406
  • 1,382
  • 5
  • 29
  • 46
0

I hope this helps. Try the following code.

uniqid(rand(),true)
Abhishek Patel
  • 774
  • 5
  • 19