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,068
  • 2
  • 22
  • 34
Shubanker
  • 2,512
  • 17
  • 23

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,278
  • 5
  • 51
  • 91
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,068
  • 2
  • 22
  • 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()`. – Shubanker Nov 21 '14 at 14:51
-4

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

Shubanker
  • 2,512
  • 17
  • 23
tylerlindell
  • 1,521
  • 2
  • 16
  • 18