0

In PHP, is there any way to get a value that is unique to the system it is running on?

Ideally, I would like it to be a sort of random string that stays the same on the system that is running on, but is different on other systems.

I need one that I don't have to store in an separate file/DB and is easily available on each script.

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
Shub
  • 2,686
  • 17
  • 26
  • define "system". Are you looking for an UUID? – The Paramagnetic Croissant Nov 21 '14 at 14:40
  • Any value string/number which is unique for each system. – Shub Nov 21 '14 at 14:41
  • PHP doesn't have a built in UUID generator that I know about, but [`uniqid()`](http://php.net/manual/en/function.uniqid.php) used in conjunction with unique hostnames may do the job for you. – Michael Berkowski Nov 21 '14 at 14:42
  • But as the manual cautions, uniqid() is not high-entropy and not to be used for any crypto purpose. Instead, http://php.net/manual/en/function.openssl-random-pseudo-bytes.php – Michael Berkowski Nov 21 '14 at 14:43
  • If you are looking for unique value for user computer, than it's not related to PHP, because PHP parameters comes from server OS/system. – Justinas Nov 21 '14 at 14:47
  • `uniqid()` is not a constant value it returns different values each time. – Shub Nov 21 '14 at 14:54
  • 1
    I would personally look up the UUID of the system like http://stackoverflow.com/questions/328936/getting-a-unique-id-from-a-unix-like-system – PeeHaa Nov 21 '14 at 14:55

3 Answers3

1

Create your own random value (with uniqid for example) and put it to environment variable. For user, apache or nginx. Or just define it in your php file.

Community
  • 1
  • 1
sectus
  • 15,605
  • 5
  • 55
  • 97
0

Yes -

Ultimately, you want gethostname()

And if you do :

echo md5($_SERVER['SERVER_ADDR'].gethostname()); 

Then you will have a unique string that pertains only to that server

But, there are plenty of other values that will be unique-ish

Try :

<?php 

print_r($_SERVER); 

?>

And you should find several values that are like what you are looking for.

Such as:

 [SERVER_SOFTWARE] => nginx/1.4.6

 [SERVER_ADDR] => 123.456.789.909

Then, there are also some predefined constants that may hold what you want -

<?php

print_r(get_defined_constants()); 


?> 

Which has things like:

[PHP_VERSION] => 5.5.9-1ubuntu4.5
[PHP_MAJOR_VERSION] => 5
[PHP_MINOR_VERSION] => 5
[PHP_RELEASE_VERSION] => 9
[PHP_EXTRA_VERSION] => -1ubuntu4.5
[PHP_VERSION_ID] => 50509

And while these values will be identical on servers that are running the same software stack -- it is often not likely that these values will match perfectly... However, your best bet for unique ID would be the IP address + the host name-

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • Well this could be the perfect solution for any real website but I am looking for an solution which should work on local server too and also should be unpredictable such as `gethostname()`. – Shub Nov 21 '14 at 14:51
-4

You could generate the uniqid() append hostname, then hash it with something like md5 or sha-256

Shub
  • 2,686
  • 17
  • 26
tylerlindell
  • 1,545
  • 2
  • 17
  • 20