本文整理匯總了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);
}
}