I want to know if this code is strong enough to prevent CSRF attack on PHP Form?
I’d say it suffices for the given purpose.
The values returned by uniqid(mt_rand(), true)
should be up to 33 bytes:
mt_rand
php_combined_lcg
However, these 33 bytes do not provide 264 bits of entropy but way less:
mt_rand
prefixThis sums up to almost 81 unknown bits. To brute force this, one would need on average 281/2 ≈ 1.2·1024 guesses that result in a given token when hashed. The data to process would be approximately 8·1013 TB. With a todays computer, you should be able to run this in approximately 5.215·1017 seconds.
This should be sufficient to render an attack impracticable.