php class写一个实用的接口
以下是一个简单的PHP类,它包含一些常见的实用接口:
class Utils {
// 返回当前时间的时间戳
public static function now() {
return time();
}
// 将字符串转换为数组
public static function stringToArray($string, $delimiter = ',') {
return explode($delimiter, $string);
}
// 将数组转换为字符串
public static function arrayToString($array, $delimiter = ',') {
return implode($delimiter, $array);
}
// 获取客户端IP地址
public static function getClientIp() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP')) {
$ipaddress = getenv('HTTP_CLIENT_IP');
} else if(getenv('HTTP_X_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
} else if(getenv('HTTP_X_FORWARDED')) {
$ipaddress = getenv('HTTP_X_FORWARDED');
} else if(getenv('HTTP_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_FORWARDED_FOR');
} else if(getenv('HTTP_FORWARDED')) {
$ipaddress = getenv('HTTP_FORWARDED');
} else if(getenv('REMOTE_ADDR')) {
$ipaddress = getenv('REMOTE_ADDR');
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
// 获取用户浏览器信息
public static function getUserAgent() {
return $_SERVER['HTTP_USER_AGENT'];
}
}
使用该类可以轻松地执行以下操作:
// 获取当前时间的时间戳
$timestamp = Utils::now();
// 将字符串转换为数组
$string = 'apple,banana,orange';
$array = Utils::stringToArray($string);
// 将数组转换为字符串
$array = ['apple', 'banana', 'orange'];
$string = Utils::arrayToString($array);
// 获取客户端IP地址
$ip = Utils::getClientIp();
// 获取用户浏览器信息
$userAgent = Utils::getUserAgent();
``
原文地址: https://www.cveoy.top/t/topic/fmu5 著作权归作者所有。请勿转载和采集!