PHP获取服务器基本信息

今天给大家分享一下我自用的PHP获取服务器信息类的代码。


<?php
namespace ServerInfo;
/**
+------------------------------------------------------------------------------
 * 获取服务器信息类

+------------------------------------------------------------------------------
 */
class ServerInfo {
//类定义开始

    /**
    +----------------------------------------------------------
     * 获取服务器时间
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetServerTime() {
        return date('Y-m-d H:i:s');
    }

    /**
    +----------------------------------------------------------
     * 获取服务器解译引擎
     * 例如:Apache/2.2.8 (Win32) PHP/5.2.6
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetServerSoftwares() {
        return $_SERVER['SERVER_SOFTWARE'];
    }

    /**
    +----------------------------------------------------------
     * 获取php版本号
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetPhpVersion() {
        return PHP_VERSION;
    }

    /**
    +----------------------------------------------------------
     * 获取Http版本号
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetHttpVersion() {
        return $_SERVER['SERVER_PROTOCOL'];
    }

    /**
    +----------------------------------------------------------
     * 获取网站根目录
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetDocumentRoot() {
        return $_SERVER['DOCUMENT_ROOT'];
    }

    /**
    +----------------------------------------------------------
     * 获取PHP脚本最大执行时间
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetMaxExecutionTime() {
        return ini_get('max_execution_time') . ' Seconds';
    }

    /**
    +----------------------------------------------------------
     * 获取服务器允许文件上传的大小
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetServerFileUpload() {
        if (@ini_get('file_uploads')) {
            return '允许 ' . ini_get('upload_max_filesize');
        } else {
            return '禁止';
        }
    }

    /**
    +----------------------------------------------------------
     * 获取全局变量 register_globals的设置信息 On/Off
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetRegisterGlobals() {
        return $this->GetPhpCfg('register_globals');
    }

    /**
    +----------------------------------------------------------
     * 获取安全模式 safe_mode的设置信息 On/Off
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetSafeMode() {
        return $this->GetPhpCfg('safe_mode');
    }

    /**
    +----------------------------------------------------------
     * 获取Gd库的版本号
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetGdVersion() {
        if (function_exists('gd_info')) {
            $GDArray = gd_info();
            $gd_version_number = $GDArray['GD Version'] ? '版本:' . $GDArray['GD Version'] : '不支持';
        } else {
            $gd_version_number = '不支持';
        }
        return $gd_version_number;
    }

    /**
    +----------------------------------------------------------
     * 获取内存占用率
    +----------------------------------------------------------
     * @access public
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    public function GetMemoryUsage() {
        return $this->ConversionDataUnit(memory_get_usage());
    }
    //服务器IP
    public function ServerIp() {
        return GetHostByName($_SERVER['SERVER_NAME']);
    }
    //服务器系统
    public function Serveros() {
        return php_uname('s');
    }

    /**
    +----------------------------------------------------------
     * 对数据单位 (字节)进行换算
    +----------------------------------------------------------
     * @access private
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    private function ConversionDataUnit($size) {
        $kb = 1024; // Kilobyte
        $mb = 1024 * $kb; // Megabyte
        $gb = 1024 * $mb; // Gigabyte
        $tb = 1024 * $gb; // Terabyte
        //round() 对浮点数进行四舍五入
        if ($size < $kb) {
            return $size . ' Byte';
        } else if ($size < $mb) {
            return round($size / $kb, 2) . ' KB';
        } else if ($size < $gb) {
            return round($size / $mb, 2) . ' MB';
        } else if ($size < $tb) {
            return round($size / $gb, 2) . ' GB';
        } else {
            return round($size / $tb, 2) . ' TB';
        }
    }

    /**
    +----------------------------------------------------------
     * 获取PHP配置文件 (php.ini)的值
    +----------------------------------------------------------
     * @param string $val 值
     * @access private
    +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     */
    private function GetPhpCfg($val) {
        switch ($result = get_cfg_var($val)) {
        case 0:
            return '关闭';
            break;
        case 1:
            return '打开';
            break;
        default:
            return $result;
            break;
        }
    }

}

最后修改:2020 年 12 月 31 日 12 : 53 PM
如果觉得我的文章对你有用,请随意赞赏

2 条评论

  1. 芭比

    看不懂OωO

    1. 晴栀
      @芭比

      这就是几个方法,有啥不懂的讷?

发表评论