You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB
PHTML

<?php
namespace Base\Tool;
/**
* @author elf<360197197@qq.com>
*/
class Request {
private $serverInfo;
private $scheme;
public function __construct()
{
$this->serverInfo = $_SERVER;
}
public function isWechat()
{
$userAgent = $this->getUserAgent();
if (strpos($userAgent, 'MicroMessenger') == false && strpos($userAgent, 'Windows Phone') == false) {
return false;
} else {
return true;
}
}
public function getUserAgent()
{
return $this->serverInfo['HTTP_USER_AGENT'] ?? '';
}
public function isMobile()
{
$isMobile = false;
$userAgent = $this->getUserAgent();
$mobileAgents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad','iPod'];
foreach ($mobileAgents as $mobileAgent) {
if (stripos($userAgent, $mobileAgent) !== false) {
$isMobile = true;
}
}
return $isMobile;
}
public function isIOS()
{
$userAgent = $this->getUserAgent();
if(stripos($userAgent, 'iphone') !== false || strpos($userAgent, 'ipad') !== false) {
return true;
}
return false;
}
public function isAndroid()
{
$userAgent = $this->getUserAgent();
5 years ago
if(stripos($userAgent, 'android')) {
return true;
}
return false;
}
public function isIOS13()
{
5 years ago
$userAgent = $this->getUserAgent();
5 years ago
// if (preg_match('/OS [1][3-9]_[1-9][_\d]* like Mac OS X/i', $userAgent)) {
if (preg_match('/OS.*[1][3-9]_[1-9][_\d]*.*like.*Mac.*OS.*X/i', $userAgent)) {
return true;
}
return false;
}
/**
* 获取客户端IP
*/
public function getClientIP()
{
$serverInfo = $this->serverInfo;
$ip = '0.0.0.0';
if (isset($serverInfo['HTTP_X_FORWARDED_FOR'])) {
$items = explode(',', $serverInfo['HTTP_X_FORWARDED_FOR']);
$position = array_search('unknown', $items);
if (false !== $position) {
unset($items[$position]);
}
$ip = trim($items[0]);
} elseif (isset($serverInfo['HTTP_CLIENT_IP'])) {
$ip = $serverInfo['HTTP_CLIENT_IP'];
} elseif (isset($this->serverInfo['REMOTE_ADDR'])) {
$ip = $serverInfo['REMOTE_ADDR'];
}
return $ip;
}
public function getHost()
{
$host = $this->getScheme() . '://' . $this->serverInfo['SERVER_NAME'];
if (in_array($this->serverInfo['SERVER_PORT'], [80, 443])) {
return $host;
}
return $host . ':' . $this->serverInfo['SERVER_PORT'];
}
public function getScheme()
{
if ($this->scheme) {
return $this->scheme;
}
$serverInfo = $this->serverInfo;
if (isset($serverInfo['REQUEST_SCHEME'])) {
$this->scheme = $serverInfo['REQUEST_SCHEME'];
return $this->scheme;
}
if ((isset($serverInfo['HTTPS']) && $serverInfo['HTTPS'] == 'on')
|| (isset($serverInfo['HTTP_X_FORWARDED_PROTO']) && $serverInfo['HTTP_X_FORWARDED_PROTO'] == 'https'
|| (isset($serverInfo['SERVER_PORT']) && $serverInfo['SERVER_PORT'] == 443))
) {
$this->scheme = 'https';
} else {
$this->scheme = 'http';
}
return $this->scheme;
}
}