You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example above, we're validating the data coming into our application. We're checking if the `name` field is a text, if the `email` field is a valid email, and if the `password` field is at least 8 characters long. If any of these validations fail, the `validate()` method will return `false` and you can get the errors using the `errors()` method. You can find the full list of validation rules [here](/docs/data/validation).
232
232
233
+
## Client IP & Geo Location
234
+
235
+
You can get the IP address of the client making the request using the `getIp()` method on the request object.
236
+
237
+
```php:no-line-numbers
238
+
request()->getIp();
239
+
```
240
+
241
+
::: danger Frontend IP Address
242
+
The IP address returned by `getIp()` may not always be the actual client IP address, especially if your application is behind a proxy or load balancer. In such cases, the IP address returned may be that of the proxy or load balancer. We have added support for the **`X-Forwarded-For`** HTTP header, which is commonly used by proxies and load balancers to pass along the original client IP address, but be sure to validate the IP address returned by `getIp()` before using it in your application for critical tasks like rate limiting or logging.
243
+
:::
244
+
245
+
### Get location from IP
246
+
247
+
You can get the geographical location of the client based on the current IP address from `getIp()` using the `getUserLocation()` method. This method uses the [ip-api](https://ip-api.com/) service to get the location data.
248
+
249
+
```php:no-line-numbers
250
+
$location = request()->getUserLocation();
251
+
252
+
// $location = [
253
+
// country' => 'United States',
254
+
// 'countryCode' => 'US',
255
+
// 'region' => 'CA',
256
+
// 'regionName' => 'California',
257
+
// 'currency' => 'USD',
258
+
// 'city' => 'San Francisco',
259
+
// 'zip' => '94105',
260
+
// 'lat' => 37.7898,
261
+
// 'lon' => -122.3942,
262
+
// 'timezone' => 'America/Los_Angeles',
263
+
// 'ip' => $ip,
264
+
// 'continent' => 'North America',
265
+
// 'continentCode' => 'NA',
266
+
```
267
+
268
+
Keep in mind that the free tier of the ip-api service has a limit of 45 requests per minute from an IP address, you can check out other implementations for more robust solutions.
269
+
270
+
### Pass in a custom IP
271
+
272
+
You can also pass in a custom IP address to the `getLocationFromIp()` method to get the location data for that IP address.
The request object comes with other methods for doing things like interacting with the request headers, cookies, checking request types, and even user data. Here are some of the most common functionality you'll use:
@@ -284,7 +329,7 @@ This section contains methods which allow you to retrieve information about the
284
329
Fetch the request’s host (e.g. “leafphp.dev”):
285
330
286
331
```php:no-line-numbers
287
-
$app->request()->getHost();
332
+
request()->getHost();
288
333
```
289
334
290
335
### Host with Port
@@ -327,14 +372,6 @@ Fetch the request’s URL (scheme + host [ + port if non-standard ]):
0 commit comments