@@ -67,13 +67,15 @@ public function setServer(string $key, string $value): static
6767 /**
6868 * Get IP
6969 *
70- * Returns users IP address.
71- * Support HTTP_X_FORWARDED_FOR header usually return
72- * from different proxy servers or PHP default REMOTE_ADDR
70+ * Extracts the client's IP address from trusted headers or falls back to the remote address.
71+ * Prioritizes headers like X-Forwarded-For when behind proxies or load balancers,
72+ * defaulting to REMOTE_ADDR when trusted headers are unavailable.
73+ *
74+ * @return string The validated client IP address or '0.0.0.0' if unavailable
7375 */
7476 public function getIP (): string
7577 {
76- $ remoteAddr = $ this ->getServer ('remote_addr ' ) ?? '0.0.0.0 ' ;
78+ $ remoteAddr = $ this ->getServer ('REMOTE_ADDR ' ) ?? '0.0.0.0 ' ;
7779
7880 foreach ($ this ->trustedIpHeaders as $ header ) {
7981 $ headerValue = $ this ->getHeader ($ header );
@@ -83,11 +85,11 @@ public function getIP(): string
8385 }
8486
8587 // Leftmost IP address is the address of the originating client
86- $ ips = \ explode (', ' , $ headerValue );
87- $ ip = \ trim ($ ips [0 ]);
88+ $ ips = explode (', ' , $ headerValue );
89+ $ ip = trim ($ ips [0 ]);
8890
8991 // Validate IP format (supports both IPv4 and IPv6)
90- if (\ filter_var ($ ip , FILTER_VALIDATE_IP )) {
92+ if (filter_var ($ ip , FILTER_VALIDATE_IP )) {
9193 return $ ip ;
9294 }
9395 }
@@ -270,29 +272,9 @@ public function getFiles($key): array
270272 */
271273 public function getCookie (string $ key , string $ default = '' ): string
272274 {
273- $ key = \strtolower ($ key );
274-
275- $ cookieHeader = $ this ->getHeader ('cookie ' , '' );
276- if ($ cookieHeader === '' ) {
277- return $ default ;
278- }
279-
280- $ cookies = \explode ('; ' , $ cookieHeader );
281- foreach ($ cookies as $ cookie ) {
282- $ cookie = \trim ($ cookie );
283- if ($ cookie === '' ) {
284- continue ;
285- }
286-
287- $ parts = \explode ('= ' , $ cookie , 2 );
288- $ cookieKey = \trim ($ parts [0 ] ?? '' );
289- $ cookieValue = \trim ($ parts [1 ] ?? '' );
290- if ($ cookieKey === $ key ) {
291- return $ cookieValue ;
292- }
293- }
275+ $ key = strtolower ($ key );
294276
295- return $ default ;
277+ return $ this -> swoole -> cookie [ $ key ] ?? $ default ;
296278 }
297279
298280 /**
@@ -338,11 +320,6 @@ public function removeHeader(string $key): static
338320 return $ this ;
339321 }
340322
341- public function getSwooleRequest (): SwooleRequest
342- {
343- return $ this ->swoole ;
344- }
345-
346323 /**
347324 * Generate input
348325 *
@@ -396,12 +373,6 @@ protected function generateInput(): array
396373 */
397374 protected function generateHeaders (): array
398375 {
399- $ headers = $ this ->swoole ->header ;
400-
401- foreach ($ headers as $ key => $ value ) {
402- $ headers [strtolower ($ key )] = $ value ;
403- }
404-
405- return $ headers ;
376+ return $ this ->swoole ->header ;
406377 }
407378}
0 commit comments