1

I am using random.org in my php script to generate random numbers like that:

    function getToken($length){
        $r = explode('
',file_get_contents('http://www.random.org/integers/?num='.$length.'&min=0&max=9&col=1&base=10&format=plain'));
        $string = '';
        foreach ( $r as $char ) $string.=$char;
        return $string;
    }

but my university net denies such queries, so whenewer i test my project using university wifi, i dont get random numbers to be generated, and that means trouble.

So, i before using this function, it needs to be chect if i can query random.org or not like that:

    if( site is accessible ) return getToken();
    else return false;

what would be the best way to check accesability?

Myself i tried:

    file_get_contents();

but it sends warnings,whenewer it fails,

    dns_get_record();

but i dont know if i can trust that, if it checks only dns name. Please help!

P.S. a pinging technique might proove usefull...

Sam
  • 7,252
  • 16
  • 46
  • 65
Ben
  • 3,989
  • 9
  • 48
  • 84

1 Answers1

1

You could just run file_get_contents with @ to supress errors and simply return when it doesn't give you a random number, resulting in something like:

function getToken($length){
    $number = @file_get_contents('http://www.random.org/integers/?num='.$length.'&min=0&max=9&col=1&base=10&format=plain');
    if($number === false) return null;
    $r = explode('',$number);
    $string = '';
    foreach ( $r as $char ) $string.=$char;
    return $string;
}

That being said, I seriously doubt if you really need to use random.org to generate random numbers. PHP's own pseudo-random generator functions include openssl_random_pseudo_bytes that is said to generate "cryptographically strong" pseudorandom numbers:

http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php

jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • i tried open_ssl, but dramaticly it's forbidden in the host i'm using – Ben Apr 26 '13 at 19:56
  • I didn't knew that @ thing, thats a usefull post. Not only you tought me how to solve the problem, but tought me something valuable. Thanks! – Ben Apr 26 '13 at 20:04
  • Yea, and as i said, i cant use open ssl, but instead i will collect generated random entries and generate only unique number combinations. – Ben Apr 26 '13 at 20:08
  • Glad you liked it. And it's ok - just go with what you can and need to use! – jsalonen Apr 26 '13 at 20:09