Hướng dẫn is php timestamp unique?

uniqid() in PHP generates a unique ID based on the current timestamp in microseconds. Is that really a foolproof way to generate a unique ID?

Even assuming there's a single user running a single script with a loop generating a timestamp in microseconds, can there still really be a theoretical guarantee that it's unqiue? And in practice, is the likelihood completely negligible?

For clarity, say your loop is nothing more than this:

foreach($things as $thing){
    var_dump(microtime());
}

is there any theoretical chance it might not be unique and, if so, how realistic is it in practice?

asked Nov 12, 2014 at 8:57

vanamerongenvanamerongen

7883 gold badges10 silver badges27 bronze badges

1

Microsecond based ids are only guaranteed to be unique within limits. A single threaded scripts on a single computer is probably pretty safe in this regard. However, as soon as you start talking about parallel execution, be that simply on multiple CPUs within the same machine or especially across multiple machines, all bets are off.

So it depends on what you want to use this id for. If you're just using it to generate an id which is used only within the same script, it's probably safe enough. For example:


You very likely won't encounter any problems here with this limited use.

However, if you start generating file names using uniqid or other such uses which are shared with other external scripts, I wouldn't rely on it. For filenames, using a hash based on the file contents may be a good idea. For general purpose decentralised randomly generated ids, UUIDs are a good fit (because they've been designed for this purpose).

answered Nov 12, 2014 at 9:05

decezedeceze

497k81 gold badges718 silver badges865 bronze badges

1

Ask yourself why you need uniqid in the first place. For instance, I use uniquid as the filename of uploads to my website. There can be any number of users who upload at the same time so what I am concerned with is two or more files having the same id, BUT I know that a single user can only upload one file at a time. So, I prepend the username in front and will always have uniqueness.

echo uniqid('username-'); // username-5621e3335ac0c

Of course, you should always ask yourself if you need to use uniquid in the first place. If you know the reason you are creating the id can only happen every x seconds, minutes, etc then you can create an id the same way just use time :

echo 'username-'.time(); // username-1445062025

answered Oct 17, 2015 at 6:14

user756659user756659

3,08011 gold badges46 silver badges104 bronze badges

1. Chức năng của hàm uniqid()

Hàm uniqid() trong PHP có chức năng tạo ra một chuỗi ký tự duy nhất dựa vào thời gian hiện tại tính theo micro giây. Chuỗi ký tự được tạo ra thường dùng làm định danh duy nhất (unique ID), có thể dùng làm khóa chính trong cơ sở dữ liệu.

2. Cú pháp của hàm uniqid()

uniqid(string $prefix = "", bool $more_entropy = false): string

Trong đó:

  • $prefix: tham số tùy chọn. Chuỗi unique ID được tạo ra được nối thêm chuỗi ký tự $prefixphía trước. Nếu tham số $prefix rỗng và $more_entropyfalse thì chuỗi trả về sẽ có 13 ký tự.
  • $more_entropy: tham số tùy chọn. Mặc định là false. Nếu $more_entropytrue thì chuỗi unique ID trả về sẽ có 23 ký tự, tăng khả năng chuỗi trả về là chuỗi duy nhất.
  • Kiểu trả về là chuỗi ký tự duy nhất theo thời gian.

Lưu ý: Hàm uniqid() trong PHP không đảm bảo 100% sẽ tạo ra chuỗi unique ID duy nhất. Trong trường hợp các tiến trình (process) hoặc tiểu trình (thread) chạy hàm uniqid() cùng lúc thì chuỗi trả về có thể giống nhau (không là duy nhất) nhưng rất hiếm khi xảy ra.

3. Một số ví dụ sử dụng hàm uniqid()

Chạy hàm uniqid() không có $prefix

$id1 = uniqid();//615aceaa9e2ce
$id2 = uniqid();//615aceaa9e2e4
//hoặc
$id1 = uniqid('', false);//615acefd5bc13
//hoặc
$id1 = uniqid('', true);//615acf1d9f5bb2.49697029

Chạy hàm uniqid() có $prefix

$id1 = uniqid('gochocit_');//gochocit_615acf5d3362e
//hoặc
$id1 = uniqid('gochocit_',false);//gochocit_615acf7def0c4
//hoặc
$id1 = uniqid('gochocit_',true);//gochocit_615acf99e4f6d9.00998831

Trong PHP, bạn có thể sử dụng những hàm như random_int(), random_bytes() hoặc openssl_random_pseudo_bytes()để thay cho uniqid(). Những hàm này giúp bạn tạo những mật mã an toàn ngẫu nhiên có tính duy nhất được đảm bảo hơn hàm uniqid() trong PHP.

  • Tạo (create) và ghi (write) file trong PHP
  • Các kỹ thuật lập trình với mảng 2 chiều và minh họa với C++
  • Truy vấn (select) dữ liệu và câu lệnh where trong MySQL với PHP
  • Hàm đệ quy (recursive function) trong Python
  • Lập trình giao tiếp cảm biến LDR với board mạch Arduino