Advertisement
carefulnow

Cross platform UUID/API key

Jul 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. <?php
  2.  
  3. class APIKey {
  4.  
  5.     public static function createKey() {
  6.         $key = null;
  7.  
  8.         do {
  9.             sprintf('%04X%04X%04X%04X%04X%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
  10.         } while (self::validateKey($key));
  11.     }
  12.  
  13.     public static function validateKey($key) {
  14.         $stmt = Database::$db->prepare("SELECT COUNT(*) FROM APIKEY WHERE uuid = ?");
  15.         $stmt->execute(array($key));
  16.         return ($stmt->fetch()[0] > 0);
  17.     }
  18.  
  19. }
  20.  
  21. $key = APIKey::createKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement