本文整理汇总了PHP中Psr\Http\Message\ServerRequestInterface::getIp方法的典型用法代码示例。如果您正苦于以下问题:PHP ServerRequestInterface::getIp方法的具体用法?PHP ServerRequestInterface::getIp怎么用?PHP ServerRequestInterface::getIp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::getIp方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* IP Restriction Middleware call.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response
* PSR7 response
* @param callable $next
* Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
// Get client's IP.
$ip = $request->getIp();
// Get the Content-Type set in the Request's Accept header.
$contentType = $this->determineContentType($request->getHeaderLine('Accept'));
// Desired status code to send.
$statusCode = $this->setStatusCode($this->options);
// Exception Message to send.
$message = $this->setExceptionMessage($contentType, $this->options);
// Restrict The IP access if needed.
$restrict = $this->restrict($ip, $this->allowList, !$this->negate);
if ($restrict === true) {
return $response->withStatus($statusCode)->withHeader('Content-type', $contentType)->write($message);
} else {
// Proceed with the application access.
return $response = $next($request, $response);
}
}