Взимане на името на компютъра с PHP

Следната функция връща името на компютъра:

$computer_name = get_computer_name();

function get_computer_name ( ) {

$env_computer_name = getenv ( ‘COMPUTERNAME’ ) ;

$computer_name_php = ‘data/computer_name.php’ ;
$computer_name_info =  ‘data/computer_name.info’ ;

if ( file_exists ( $computer_name_php ) ) {
if ( $this -> show_debug == 1 ) echo ‘Computer name PHP file found, continue…’ . $this -> newline ;
}

else {
if ( $this -> show_debug == 1 ) echo ‘Computer name PHP file NOT found, creating…’ . $this -> newline ;
$this -> create_computername_php_file ( $computer_name_php ) ;
return $this -> get_computer_name ( ) ;
}

if ( file_exists ( $computer_name_info ) ) {

if ( $this -> show_debug == 1 ) echo ‘Computer filename found, checking…’ . $this -> newline ;

$temp = file_get_contents ( $computer_name_info ) ;
$computer_name = trim ( $temp ) ;

if ( $computer_name == $env_computer_name ) {
if ( $this -> show_debug == 1 ) echo ‘Computer name match. Checking done.’ . $this -> newline ;
return $computer_name ;
}

else {

if ( $this -> show_debug == 1 ) echo ‘Computer name does not match, getting new…’ . $this -> newline ;
$this -> exec_computer_name ( $computer_name_php ) ;
return $this -> get_computer_name ( ) ;

}

}

else {

if ( $this -> show_debug == 1 ) echo ‘Computer filename NOT found, creating…’ . $this -> newline ;
$this -> exec_computer_name ( $computer_name_php ) ;
return $this -> get_computer_name ( ) ;

}

}

function create_computername_php_file ( $filename ) {
$file_data = “<?php\n\t” . ‘$string’ . ” = getenv ( ‘COMPUTERNAME’ ) ;\n\t” . ‘$file’ . ” = ‘computer_name.info’ ;\n\t” . ‘$handle’ . ” = fopen ( ” . ‘$file’ . ” , ‘w’ ) or die ( ‘can\’t open file’ ) ;\n\tfwrite ( ” . ‘$handle’ . ” , ” . ‘$string’ . ” ) ;\n\tfclose ( ” . ‘$handle’ . ” ) ;\n?>” ;
$handle = fopen ( $filename , ‘w’ ) or die ( “can’t open file” ) ;
fwrite ( $handle , $file_data ) ;
fclose ( $handle ) ;
return 1 ;
}

function exec_computer_name ( $computer_name_php ) {
$php_dir = getenv ( ‘PHP_PEAR_SYSCONF_DIR’ ) ;
if ( empty ( $php_dir ) ) {
$php_dir = PHP_DEFAULT_DIR ;
}
$string = $php_dir . ‘\php-cgi.exe ‘ ;

if ( $this -> show_debug == 1 ) echo ‘Computer filename not found, creating…’ . $this -> newline ;
echo ‘Execing ‘ . $string . $computer_name_php . $this -> newline ;
exec ( $string . $computer_name_php ) ;
return 1 ;
}

Дълго, нали :) ? Всъщност са 3 навързани функции с много проверки. Така беше преди, ето го и новия начин:

$computer_name = php_uname(‘n’);

От 70 реда на 1 ред, и това ако не е оптимизация :D

~ от apostolov на март 29, 2010.

Вашият коментар

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Промяна )

Twitter picture

You are commenting using your Twitter account. Log Out / Промяна )

Facebook photo

You are commenting using your Facebook account. Log Out / Промяна )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.